2012-09-30 102 views
1

我在网上搜索了几天,但可以弄清楚如何让这段代码工作。这是一个非常简单的GUI(用Qt Designer制作),带有一个LCD和一个按钮。我希望它在按下按钮后从180秒开始倒计时。在第一时间,我能够使按钮减少一个值,但尝试了很多不同的东西后,没有任何工作。有人能帮助我吗?可能是非常简单的事情。谢谢。似乎无法让pyqt倒数计时器正常工作

# -*- coding: utf-8 -*- 
import sys 
import time 
from PyQt4 import QtCore, QtGui 
from relogio import Ui_relogiocc 

class StartQT4(QtGui.QMainWindow): 
def __init__(self, parent=None): 
    QtGui.QWidget.__init__(self, parent) 
    self.ui = Ui_relogiocc() 
    self.ui.setupUi(self) 
    self.timer = QtCore.QTimer() 
    text = "%d:%02d" % (180/60,180 % 60) 
    self.ui.QLCDNumber.display(text) 
    self.timer.start(1000) 
    self.ui.iniciar.clicked.connect(self.updateTimerDisplay) 


def updateTimerDisplay(self): 
    self.inicio = 180 
    while self.inicio != 0: 
    text = "%d:%02d" % (self.inicio/60,self.inicio % 60) 
    self.ui.QLCDNumber.display(text) 
    self.inicio - 1 
    else: 
    self.timer.stop() 


if __name__ == "__main__": 
app = QtGui.QApplication(sys.argv) 
myapp = StartQT4() 
myapp.show() 
sys.exit(app.exec_()) 

回答

1

看起来有很多东西你在这里失踪。

首先,每当在每个超时期内完成计时器发出timeout()信号。就你而言,这是每一秒钟。但是,你并没有把这个信号连接到任何东西。

其次,你updateTimerDisplay包含以下行:

self.inicio - 1 

这读取self.inicio值,从中减去1,然后抛出的结果了。因为self.inicio的值不会改变,您的updateTimerDisplay方法将进入无限循环。

我猜你的意思

self.inicio -= 1 

来代替,该分配的self.inicio新值回发到自身。

但是,最终,您似乎试图使用updateTimerDisplay方法启动计时器,将其计数并更新计时器的显示。我建议将这种方法分解为更小的方法。

首先,updateTimerDisplay应该只更新计时器的显示:

def updateTimerDisplay(self): 
    text = "%d:%02d" % (self.inicio/60,self.inicio % 60) 
    self.ui.QLCDNumber.display(text) 

其次,你要开始计时的方法。像下面这样的东西应该做的事:

def startTimer(self): 
    self.inicio = 180 
    self.updateTimerDisplay() 
    self.timer.start(1000) 

当然,你还需要你iniciar按钮的clicked()信号连接到这个功能,而不是向updateTimerDisplay

最后,您需要一个方法来处理来自计时器的记号。像下面这样的东西应该做的事:

def timerTick(self): 
    self.inicio -= 1 
    self.updateTimerDisplay() 
    if self.inicio <= 0: 
    self.timer.stop() 

您还需要连接定时器此功能的timeout()信号,使用类似:

self.timer.timeout.connect(self.timerTick) 
+0

谢谢你的非常好的解释。 –

0

您的代码有多个错误。对于初学者,你写了self.inicio - 1而不是-= 1,你永远不会使用你创建的Qtimer。但是忽略这一点,你的程序结构是不正确的:当你点击你的iniciar按钮并在那里循环时,你目前拨打updateTimerDisplay,直到你的倒数到零。你想要做的是开始计时,当用户点击该按钮并连接定时器(实际上它timeout信号),只是倒计数一秒钟更新显示的方法:

def startTimerDisplay(self): 
    """ set the countdown value and start the timer """ 
    self.inicio = 180 
    self.timer.start(1000) 

def updateTimerDisplay(self): 
    """ count down one second, set the text, and check if the timer should stop """ 
    self.inicio -= 1 
    text = "%d:%02d" % (self.inicio/60,self.inicio % 60) 
    self.ui.QLCDNumber.display(text) 
    if self.inicio == 0: 
     self.timer.stop() 

改变你__init__方法连接这些功能是这样的:

def __init__(self, parent=None): 
    QtGui.QWidget.__init__(self, parent) 
    self.ui = Ui_relogiocc() 
    self.ui.setupUi(self) 
    self.timer = QtCore.QTimer() 
    text = "%d:%02d" % (180/60,180 % 60) 
    self.ui.QLCDNumber.display(text) 
    #connect the button to the start method ... 
    self.ui.iniciar.clicked.connect(self.startTimerDisplay) 
    #... and the timer to the update method 
    self.timer.timeout.connect(self.updateTimerDisplay) 
+0

谢谢!这是我第一次使用Qt ...现在工作了! –

0

,因为它在其他被告知回答你的代码包含一些明显的错误。这里有一个完整的工作示例(用户界面不是通过Designer创建的),每次单击该按钮时都会正确复位计数器(即在再次启动计时器之前停止计时器,如果不这样做并单击前面的Start按钮计时器已停止,然后计数器每按一次按钮计数就会更快)。

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class MyMainWindow(QMainWindow): 
    def __init__(self, parent=None): 
     QMainWindow.__init__(self, parent) 

     self.central = QWidget(self) 
     self.hbox = QHBoxLayout(self.central) 
     self.lcd = QLCDNumber(self.central) 
     self.timer = QTimer(self) 
     self.start_time = 20 
     self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60)) 
     self.start_button = QPushButton("Start", self.central) 
     self.hbox.addWidget(self.lcd) 
     self.hbox.addWidget(self.start_button) 
     self.setCentralWidget(self.central) 

     self.start_button.clicked.connect(self.restartTimer) 
     self.timer.timeout.connect(self.updateLCD) 

    def restartTimer(self): 
     # Reset the timer and the lcd 
     self.timer.stop() 
     self.start_time = 20 
     self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60)) 
     # Restart the timer 
     self.timer.start(1000) 

    def updateLCD(self): 
     # Update the lcd 
     self.start_time -= 1 
     if self.start_time >= 0: 
      self.lcd.display("%d:%02d" % (self.start_time/60,self.start_time % 60)) 
     else: 
      self.timer.stop() 

if __name__ == "__main__": 
    import sys 
    app = QApplication(sys.argv) 
    ui = MyMainWindow() 
    ui.show() 
    sys.exit(app.exec_()) 
相关问题