繁体   English   中英

PySide2如何像time.sleep一样使用Qtimer

[英]PySide2 how to use Qtimer like time.sleep

我有一个简单的 GUI 应用程序,它是一个带 LCD 显示屏的计时器 我在延迟一个 function 时遇到问题,该 function 负责倒计时它应该每秒倒计时的时间,但它在一秒钟内计算分钟,它用于循环 function 这里是演示项目的

from PySide2 import QtWidgets , QtCore
import sys
from clock_gui import Ui_clock # this is the GUI components

class clockApp(QtWidgets.QMainWindow, Ui_clock):
    def __init__(self):
        super(clockApp,self).__init__()
        self.setupUi(self) # it set up the UI from Ui_clock
        self.min = self.spinBox_min.value()
        self.sec = self.spinBox_sec.value()
        self.button.clicked.connect(self._buttonFunction) # the button 
        self.show() # render the GUI


    def _buttonFunction(self): # the function that counts down after the button clicked 
        self.total = (self.min *60) + self.sec # take minutes and seconds to one variable  
        if self.total >=1:
           for i in range(self.total):
               self.total -= 1            
               # here the problem I want the delay to start here
               # I can use time.sleep but I want to use Qtimer I try it in different ways, but didn't work there is no error but the Qtimer didn't delay anything 


               




app = QtWidgets.QApplication(sys.argv)
window  = clockApp()
app.exec_()

我尝试 setInterval 并尝试 singleShot 但没有任何效果,也许我用错了?

使用QTimer是正确的方法,但您需要使用超时信号而不是 for 循环(它将阻止 GUI 更新直到结束)。 使用QTime object 倒计时也可能会有所帮助,因为这样可以更轻松地格式化 LCD 小部件的 dsiplay。

下面是一个简单的演示,展示了一种实现方式。 希望您能看到如何调整此代码以满足您自己的要求:

截屏

from PySide2 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.handleTimeout)
        self.buttonStart = QtWidgets.QPushButton('Start')
        self.buttonStart.clicked.connect(self.handleStart)
        self.buttonStop = QtWidgets.QPushButton('Stop')
        self.buttonStop.clicked.connect(self.timer.stop)
        self.min = QtWidgets.QSpinBox()
        self.min.setRange(0, 10)
        self.sec = QtWidgets.QSpinBox()
        self.sec.setRange(0, 59)
        self.lcd = QtWidgets.QLCDNumber()
        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.lcd, 0, 0, 1, 2)
        layout.addWidget(self.min, 1, 0)
        layout.addWidget(self.sec, 1, 1)
        layout.addWidget(self.buttonStart, 2, 0)
        layout.addWidget(self.buttonStop, 2, 1)
        self.time = QtCore.QTime(0, 0)
        self.handleTimeout()

    def handleStart(self):
        if not self.timer.isActive():
            self.time.setHMS(0, self.min.value(), self.sec.value())
            self.handleTimeout()
            self.timer.start()

    def handleTimeout(self):
        self.lcd.display(self.time.toString('m:ss'))
        if self.time.minute() or self.time.second():
            self.time = self.time.addSecs(-1)
        else:
            self.timer.stop()

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    app.exec_()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM