PyQt倒数计时器,mm:ss格式

问题描述 投票:0回答:1

因此,我试图创建一个简单的GUI UI,它将在启动时倒计时。我有工作的代码,但不确定如何以MM:SS格式开始时间。

这是我的GUI

enter image description here

当我单击“开始”时,标签更改为60(显然)。

enter image description here

目标是将其设置为固定时间,例如10分钟,并使其从10:00开始,然后下降到09:59、09:58,等等。>

这是我到目前为止的代码:

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_())

因此,我试图创建一个简单的GUI UI,它将在启动时倒计时。我有工作的代码,但不确定如何以MM:SS格式开始时间。这是我的GUI,当我单击“开始”时,...

python pyqt
1个回答
0
投票

要回答您的特定问题,您需要先将self.time_left_int值转换为格式化的字符串,然后再更新self.timerLabel小部件中的文本。这是执行该转换的函数。

© www.soinside.com 2019 - 2024. All rights reserved.