如何阻止PyQt计时器不断重置

时间:2016-05-01 18:27:12

标签: python python-2.7 timer pyqt pyqt4

我正在尝试为项目创建一个PyQt计时器应用程序。我使用QtDesigner设计了布局,我几乎完成了所有代码。只有一个小问题。我想为我的计时器制作一个停止按钮,但每次我做它时,只要我再次按下启动计时器就会重置。只要计时器还没有超时,我希望它继续。我做了一次部分修复,这意味着计时器确实继续,但问题是数字进入负数而不是仅停在零。我是Python的新手,对PyQt来说更是如此,所以如果解决方案看起来很明显,我很抱歉(我花了好几天试图解决它)。

无论如何这里是代码(它自己重置的代码):

from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(351, 200)
        MainWindow.setMinimumSize(QtCore.QSize(0, 200))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../Pictures/clock-icon-md.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.display = QtGui.QLCDNumber(self.centralwidget)
        font = QtGui.QFont()
        font.setKerning(True)
        self.display.setFont(font)
        self.display.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.display.setAutoFillBackground(False)
        self.display.setFrameShape(QtGui.QFrame.Box)
        self.display.setFrameShadow(QtGui.QFrame.Raised)
        self.display.setMidLineWidth(-2)
        self.display.setSmallDecimalPoint(False)
        self.display.setNumDigits(5)
        self.display.setDigitCount(5)
        self.display.setMode(QtGui.QLCDNumber.Dec)
        self.display.setSegmentStyle(QtGui.QLCDNumber.Filled)
        self.display.setProperty("value", 0.0)
        self.display.setProperty("intValue", 0)
        self.display.setObjectName(_fromUtf8("display"))
        self.gridLayout.addWidget(self.display, 0, 0, 1, 5)
        self.minBox = QtGui.QSpinBox(self.centralwidget)
        self.minBox.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.minBox.setMaximum(60)
        self.minBox.setObjectName(_fromUtf8("minBox"))
        self.gridLayout.addWidget(self.minBox, 2, 0, 1, 1)
        self.secBox = QtGui.QSpinBox(self.centralwidget)
        self.secBox.setMaximum(60)
        self.secBox.setObjectName(_fromUtf8("secBox"))
        self.gridLayout.addWidget(self.secBox, 2, 1, 1, 1)
        self.secLabel = QtGui.QLabel(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.secLabel.sizePolicy().hasHeightForWidth())
        self.secLabel.setSizePolicy(sizePolicy)
        self.secLabel.setObjectName(_fromUtf8("secLabel"))
        self.gridLayout.addWidget(self.secLabel, 3, 1, 1, 1)
        self.minLabel = QtGui.QLabel(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.minLabel.sizePolicy().hasHeightForWidth())
        self.minLabel.setSizePolicy(sizePolicy)
        self.minLabel.setObjectName(_fromUtf8("minLabel"))
        self.gridLayout.addWidget(self.minLabel, 3, 0, 1, 1)
        self.startBtn = QtGui.QPushButton(self.centralwidget)
        self.startBtn.setObjectName(_fromUtf8("startBtn"))
        self.gridLayout.addWidget(self.startBtn, 2, 2, 1, 1)
        self.ResetBtn = QtGui.QPushButton(self.centralwidget)
        self.ResetBtn.setWhatsThis(_fromUtf8(""))
        self.ResetBtn.setObjectName(_fromUtf8("ResetBtn"))
        self.gridLayout.addWidget(self.ResetBtn, 2, 4, 1, 1)
        self.stopBtn = QtGui.QPushButton(self.centralwidget)
        self.stopBtn.setObjectName(_fromUtf8("stopBtn"))
        self.gridLayout.addWidget(self.stopBtn, 2, 3, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 351, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.msgBox = QtGui.QMessageBox()
        self.msgBox.setWindowTitle('Finished')
        self.msgBox.setIcon (QtGui.QMessageBox.Information)
        self.msgBox.setText("Time Out !!")
        stopButton = self.msgBox.addButton("Stop", QtGui.QMessageBox.ActionRole)
        ignoreButton = self.msgBox.addButton(QtGui.QMessageBox.Ignore)

        self.started = False

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.home()

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Timer Application", None))
        self.secLabel.setText(_translate("MainWindow", "Seconds", None))
        self.minLabel.setText(_translate("MainWindow", "Minutes", None))
        self.startBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Start the timer</p></body></html>", None))
        self.startBtn.setStatusTip(_translate("MainWindow", "Start the timer", None))
        self.startBtn.setText(_translate("MainWindow", "Start", None))
        self.ResetBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Reset the timer</p></body></html>", None))
        self.ResetBtn.setStatusTip(_translate("MainWindow", "Reset the timer", None))
        self.ResetBtn.setText(_translate("MainWindow", "Reset", None))
        self.stopBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Stop the timer</p></body></html>", None))
        self.stopBtn.setStatusTip(_translate("MainWindow", "Stop the timer", None))
        self.stopBtn.setText(_translate("MainWindow", "Stop", None))

    def home(self):
        self.startBtn.clicked.connect(self.value_calc)
        self.stopBtn.clicked.connect(self.stop_timer)
        self.ResetBtn.clicked.connect(self.reset_timer)



    def stop_timer(self):
        self.timer.stop()
        if self.value != -1:
            self.started = True
        else:
            self.started = False



    def displayer(self):
        self.display.display(self.value)
        self.value -= 1


    if self.value == -1:
            self.display.display(0)
            self.timer.stop()

            self.msgBox.show()




    def value_calc(self):
        sec_value = self.secBox.value()
        min_value = self.minBox.value()
        min_value *= 60
        self.value = sec_value + min_value
        if self.value != -1:
            if self.started == False:
                self.timer = QtCore.QTimer()
                self.timer.timeout.connect(self.displayer)
                self.timer.start(1000)
            else:
                self.timer.start(1000)
        else:
            pass

    def reset_timer(self):
        self.timer.stop()
        self.display.display(0)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

程序中有很多不必要的if语句和一个名为self.started的不必要变量。您还需要一种方法告诉value_calc函数检查self.value的当前值,这只能通过在self.value函数中设置home的默认值来实现(你实际上应该使用__init__)当你按下重置按钮时你还需要将self.value重置回它的初始值,这样你就不会从它停止的地方开始计数,这也是阻止您的计时器计入负值,因为value_calc始终会检查self.value是否小于零

以下是最终代码:

from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(351, 200)
        MainWindow.setMinimumSize(QtCore.QSize(0, 200))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../Pictures/clock-icon-md.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.display = QtGui.QLCDNumber(self.centralwidget)
        font = QtGui.QFont()
        font.setKerning(True)
        self.display.setFont(font)
        self.display.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.display.setAutoFillBackground(False)
        self.display.setFrameShape(QtGui.QFrame.Box)
        self.display.setFrameShadow(QtGui.QFrame.Raised)
        self.display.setMidLineWidth(-2)
        self.display.setSmallDecimalPoint(False)
        self.display.setNumDigits(5)
        self.display.setDigitCount(5)
        self.display.setMode(QtGui.QLCDNumber.Dec)
        self.display.setSegmentStyle(QtGui.QLCDNumber.Filled)
        self.display.setProperty("value", 0.0)
        self.display.setProperty("intValue", 0)
        self.display.setObjectName(_fromUtf8("display"))
        self.gridLayout.addWidget(self.display, 0, 0, 1, 5)
        self.minBox = QtGui.QSpinBox(self.centralwidget)
        self.minBox.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.minBox.setMaximum(60)
        self.minBox.setObjectName(_fromUtf8("minBox"))
        self.gridLayout.addWidget(self.minBox, 2, 0, 1, 1)
        self.secBox = QtGui.QSpinBox(self.centralwidget)
        self.secBox.setMaximum(60)
        self.secBox.setObjectName(_fromUtf8("secBox"))
        self.gridLayout.addWidget(self.secBox, 2, 1, 1, 1)
        self.secLabel = QtGui.QLabel(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.secLabel.sizePolicy().hasHeightForWidth())
        self.secLabel.setSizePolicy(sizePolicy)
        self.secLabel.setObjectName(_fromUtf8("secLabel"))
        self.gridLayout.addWidget(self.secLabel, 3, 1, 1, 1)
        self.minLabel = QtGui.QLabel(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.minLabel.sizePolicy().hasHeightForWidth())
        self.minLabel.setSizePolicy(sizePolicy)
        self.minLabel.setObjectName(_fromUtf8("minLabel"))
        self.gridLayout.addWidget(self.minLabel, 3, 0, 1, 1)
        self.startBtn = QtGui.QPushButton(self.centralwidget)
        self.startBtn.setObjectName(_fromUtf8("startBtn"))
        self.gridLayout.addWidget(self.startBtn, 2, 2, 1, 1)
        self.ResetBtn = QtGui.QPushButton(self.centralwidget)
        self.ResetBtn.setWhatsThis(_fromUtf8(""))
        self.ResetBtn.setObjectName(_fromUtf8("ResetBtn"))
        self.gridLayout.addWidget(self.ResetBtn, 2, 4, 1, 1)
        self.stopBtn = QtGui.QPushButton(self.centralwidget)
        self.stopBtn.setObjectName(_fromUtf8("stopBtn"))
        self.gridLayout.addWidget(self.stopBtn, 2, 3, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 351, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.msgBox = QtGui.QMessageBox()
        self.msgBox.setWindowTitle('Finished')
        self.msgBox.setIcon (QtGui.QMessageBox.Information)
        self.msgBox.setText("Time Out !!")
        stopButton = self.msgBox.addButton("Stop", QtGui.QMessageBox.ActionRole)
        ignoreButton = self.msgBox.addButton(QtGui.QMessageBox.Ignore)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.home()

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Timer Application", None))
        self.secLabel.setText(_translate("MainWindow", "Seconds", None))
        self.minLabel.setText(_translate("MainWindow", "Minutes", None))
        self.startBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Start the timer</p></body></html>", None))
        self.startBtn.setStatusTip(_translate("MainWindow", "Start the timer", None))
        self.startBtn.setText(_translate("MainWindow", "Start", None))
        self.ResetBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Reset the timer</p></body></html>", None))
        self.ResetBtn.setStatusTip(_translate("MainWindow", "Reset the timer", None))
        self.ResetBtn.setText(_translate("MainWindow", "Reset", None))
        self.stopBtn.setToolTip(_translate("MainWindow", "<html><head/><body><p>Stop the timer</p></body></html>", None))
        self.stopBtn.setStatusTip(_translate("MainWindow", "Stop the timer", None))
        self.stopBtn.setText(_translate("MainWindow", "Stop", None))

    def home(self):
        self.startBtn.clicked.connect(self.value_calc)
        self.stopBtn.clicked.connect(self.stop_timer)
        self.ResetBtn.clicked.connect(self.reset_timer)
        self.value = -1



    def stop_timer(self):
        self.timer.stop()


    def displayer(self):
        self.display.display(self.value)
        self.value -= 1

        if self.value == -1:
            self.display.display(0)
            self.timer.stop()
            self.msgBox.show()




    def value_calc(self):
        if self.value < 0:
            sec_value = self.secBox.value()
            min_value = self.minBox.value()
            min_value *= 60
            self.value = sec_value + min_value
            self.timer = QtCore.QTimer()
            self.timer.timeout.connect(self.displayer)
            self.timer.start(1000)
        else:
            self.timer.start(1000)

    def reset_timer(self):
        self.timer.stop()
        self.display.display(0)
        self.value = -1

也适用于下次比较python中的布尔值

if x == Trueif x == False

相当于

if xif not x