1

我已经在互联网上搜索了几天,但可以弄清楚如何让这段代码工作。这是一个非常简单的 gui(在 Qt Designer 上制作),带有一个 lcd 和一个按钮。我希望它在按下按钮时从 180 秒开始倒计时。在第一刻,我能够使按钮减少一个值,但是在尝试了这么多不同的东西之后,没有任何效果。有人能帮助我吗?可能是非常简单的事情。谢谢你。

# -*- 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_())
4

3 回答 3

1

正如其他答案中所说,您的代码包含一些明显的错误。在这里,您有一个完整的工作示例(UI 不是通过设计器创建的),每次单击按钮时都会正确重置计数器(即在再次启动之前停止计时器。如果您不这样做并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_())
于 2012-09-30T14:10:42.657 回答
1

It seems there are a number of things you are missing here.

Firstly, the timer emits a timeout() signal whenever each timeout period completes. In your case, this is every one second. However, you're not connecting this signal to anything.

Secondly, your updateTimerDisplay contains the following line:

   self.inicio - 1

This reads the value of self.inicio, subtracts 1 from it and then throws the result away. Because self.inicio's value doesn't change your updateTimerDisplay method goes into an infinite loop.

I'm guessing you meant

   self.inicio -= 1

instead, which assigns the new value of self.inicio back to itself.

Ultimately, however, it seems you're trying to use your updateTimerDisplay method to start the timer, count it down and also update the display of the timer. I'd recommend breaking this method up to smaller methods.

Firstly, updateTimerDisplay should only update the display of the timer:

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

Secondly, you'll want a method to start the timer. Something like the following should do:

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

Of course, you'll also need to connect your iniciar button's clicked() signal to this function, instead of to updateTimerDisplay.

Finally, you'll need a method that handles a tick from the timer. Something like the following should do:

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

You'll also need to connect the timeout() signal of the timer to this function, using something like:

  self.timer.timeout.connect(self.timerTick)
于 2012-09-30T13:23:09.710 回答
0

There are multiple things wrong with your code. For starters, you wrote self.inicio - 1 instead of -= 1, and you never actually use the Qtimer you create. But ignoring that, the structure of your program isn't right: Currently you call updateTimerDisplay when the user clicks your iniciar button and loop in there until your countdown reaches zero. What you want to do is start the timer when the user clicks the button and connect the timer (actually its timeout signal) to a method that just counts down one second and updates the display:

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

Alter your __init__ method to connect these functions like this:

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)
于 2012-09-30T13:22:42.030 回答