So I've trying to create a simple GUI UI that will countdown on start. I have a working code I worked on but I'm not sure how to start the time in a MM:SS format.
This is my GUI

When I click "start", the label changes to 60 (obviously).

The goal is to set it to a fixed time, like 10 minutes, and for it to start at 10:00, and go down to 09:59, 09:58, etc.
This is the code I have so far:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore
import sys
DURATION_INT = 60
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# App window
self.app = QApplication(sys.argv)
self.win = QMainWindow()
self.win.setGeometry(200, 200, 200, 200)
self.win.setWindowTitle("test")
# Widgets
self.titleLabel = QtWidgets.QLabel(self.win)
self.titleLabel.setText("Welcome to my app")
self.titleLabel.move(50,20)
self.timerLabel = QtWidgets.QLabel(self.win)
self.timerLabel.setText("01:00")
self.timerLabel.move(50,50)
self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
self.timerLabel.setStyleSheet("font: 25pt Helvetica")
self.startButton = QtWidgets.QPushButton(self.win)
self.startButton.setText("Start")
self.startButton.move(50,100)
self.startButton.clicked.connect(self.startTimer)
self.stopButton = QtWidgets.QPushButton(self.win)
self.stopButton.setText("Stop")
self.stopButton.move(50,130)
# Show window
self.win.show()
sys.exit(app.exec_())
def startTimer(self):
self.time_left_int = DURATION_INT
self.myTimer = QtCore.QTimer(self)
self.myTimer.timeout.connect(self.timerTimeout)
self.myTimer.start(1000)
def timerTimeout(self):
self.time_left_int -= 1
if self.time_left_int == 0:
self.time_left_int = DURATION_INT
self.update_gui()
def update_gui(self):
self.timerLabel.setText(str(self.time_left_int))
app = QtWidgets.QApplication(sys.argv)
main_window = App()
main_window.show()
sys.exit(app.exec_())
To answer your specific question, you need to convert the self.time_left_int value to a formatted string before updating the text in the self.timerLabel widget. Here is a function that performs that conversion.
def secs_to_minsec(secs: int):
mins = secs // 60
secs = secs % 60
minsec = f'{mins:02}:{secs:02}'
return minsec
Another issue with your program is that you define the self.time_left_int and self.myTimer variables in the member function startTimer. Since these are member variables of the App class, they must instead be defined in __init__().
Below is a complete version of your program that corrects these issues. Other changes I made are:
self.timerLabel in order to keep it from exceeding the size of its frame on my machine; you may need to change it back for your own purposes.DURATION_INT to 600 seconds instead of 60 so it will count down from 10:00 instead of 01:00.self.update_gui() near the end of __init__(), in order to cause the initial value of DURATION_INT to appear upon program launch.from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore
import sys
DURATION_INT = 600
def secs_to_minsec(secs: int):
mins = secs // 60
secs = secs % 60
minsec = f'{mins:02}:{secs:02}'
return minsec
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.time_left_int = DURATION_INT
self.myTimer = QtCore.QTimer(self)
# App window
self.app = QApplication(sys.argv)
self.win = QMainWindow()
self.win.setGeometry(200, 200, 200, 200)
self.win.setWindowTitle("test")
# Widgets
self.titleLabel = QtWidgets.QLabel(self.win)
self.titleLabel.setText("Welcome to my app")
self.titleLabel.move(50,20)
self.timerLabel = QtWidgets.QLabel(self.win)
self.timerLabel.setText("01:00")
self.timerLabel.move(50,50)
self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
self.timerLabel.setStyleSheet("font: 10pt Helvetica")
self.startButton = QtWidgets.QPushButton(self.win)
self.startButton.setText("Start")
self.startButton.move(50,100)
self.startButton.clicked.connect(self.startTimer)
self.stopButton = QtWidgets.QPushButton(self.win)
self.stopButton.setText("Stop")
self.stopButton.move(50,130)
self.update_gui()
# Show window
self.win.show()
sys.exit(app.exec_())
def startTimer(self):
self.time_left_int = DURATION_INT
self.myTimer.timeout.connect(self.timerTimeout)
self.myTimer.start(1000)
def timerTimeout(self):
self.time_left_int -= 1
if self.time_left_int == 0:
self.time_left_int = DURATION_INT
self.update_gui()
def update_gui(self):
minsec = secs_to_minsec(self.time_left_int)
self.timerLabel.setText(minsec)
app = QtWidgets.QApplication(sys.argv)
main_window = App()
main_window.show()
sys.exit(app.exec_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With