简体   繁体   中英

PyQt5 QLCD Countdown

i am working to build an app with countdown timer for gui see the Picture below:

在此处输入图像描述

On the bottom left you set the time. The orange button is to put the start time into the programm as well as into the green lcd. With the start Button I am trying to start the green lcd to countdown the seconds. However I cannot get this to work. The Programm just Exits doing nothing. I hope you can check my code an tell me what I Need to Change. EDIT: Tried to make it happen with a timer. Just displaying the atcual time. When running the updateLCD the app just find the new Code for gui and the code for Control below.

It seems that the.display() function kills the app. If I comment it out Nothing happens but the app stays open. Thanks a lot in Advance.

First.pyw code: from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_set_time(object):
    def setupUi(self, set_time):
        set_time.setObjectName("set_time")
        set_time.resize(1125, 824)
        self.job_time = QtWidgets.QTimeEdit(set_time)
        self.job_time.setGeometry(QtCore.QRect(10, 770, 231, 41))
        self.job_time.setStyleSheet("font: 75 16pt \"MS Shell Dlg 2\";")
        self.job_time.setTime(QtCore.QTime(0, 0, 0))
        self.job_time.setObjectName("job_time")
        self.start_button = QtWidgets.QPushButton(set_time)
        self.start_button.setGeometry(QtCore.QRect(960, 40, 150, 90))
        self.start_button.setStyleSheet("font: 75 22pt \"MS Shell Dlg 2\";\n"
"background-color: rgb(0, 255, 0);")
        self.start_button.setAutoDefault(False)
        self.start_button.setDefault(False)
        self.start_button.setFlat(False)
        self.start_button.setObjectName("start_button")
        self.pause_button = QtWidgets.QPushButton(set_time)
        self.pause_button.setGeometry(QtCore.QRect(960, 140, 150, 90))
        self.pause_button.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"font: 75 22pt \"MS Shell Dlg 2\";")
        self.pause_button.setObjectName("pause_button")
        self.finish_button = QtWidgets.QPushButton(set_time)
        self.finish_button.setGeometry(QtCore.QRect(960, 240, 150, 90))
        self.finish_button.setStyleSheet("background-color: rgb(0, 85, 255);\n"
"font: 75 22pt \"MS Shell  2\";\n"
"color: rgb(255, 255, 255);")
        self.finish_button.setObjectName("finish_button")
        self.label = QtWidgets.QLabel(set_time)
        self.label.setGeometry(QtCore.QRect(10, 720, 231, 40))
        self.label.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label.setLineWidth(10)
        self.label.setMidLineWidth(10)
        self.label.setObjectName("label")
        self.kill_Button = QtWidgets.QPushButton(set_time)
        self.kill_Button.setGeometry(QtCore.QRect(960, 720, 150, 90))
        self.kill_Button.setObjectName("kill_Button")
        self.label_2 = QtWidgets.QLabel(set_time)
        self.label_2.setGeometry(QtCore.QRect(260, 720, 261, 40))
        self.label_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label_2.setLineWidth(10)
        self.label_2.setMidLineWidth(10)
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(set_time)
        self.lineEdit.setGeometry(QtCore.QRect(260, 770, 261, 41))
        self.lineEdit.setObjectName("lineEdit")
        self.remaining_time = QtWidgets.QLCDNumber(set_time)
        self.remaining_time.setGeometry(QtCore.QRect(0, 50, 911, 221))
        self.remaining_time.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(0, 255, 0);")
        self.remaining_time.setDigitCount(8)
        self.remaining_time.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.remaining_time.setObjectName("remaining_time")
        self.label_3 = QtWidgets.QLabel(set_time)
        self.label_3.setGeometry(QtCore.QRect(0, 10, 911, 40))
        self.label_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label_3.setLineWidth(10)
        self.label_3.setMidLineWidth(10)
        self.label_3.setObjectName("label_3")
        self.extra_time = QtWidgets.QLCDNumber(set_time)
        self.extra_time.setGeometry(QtCore.QRect(0, 320, 911, 221))
        self.extra_time.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 0, 0);")
        self.extra_time.setDigitCount(8)
        self.extra_time.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.extra_time.setObjectName("extra_time")
        self.label_4 = QtWidgets.QLabel(set_time)
        self.label_4.setGeometry(QtCore.QRect(0, 280, 911, 40))
        self.label_4.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label_4.setLineWidth(10)
        self.label_4.setMidLineWidth(10)
        self.label_4.setObjectName("label_4")
        self.set_button = QtWidgets.QPushButton(set_time)
        self.set_button.setGeometry(QtCore.QRect(530, 720, 150, 90))
        self.set_button.setStyleSheet("background-color: rgb(255, 170, 0);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
        self.set_button.setObjectName("set_button")

        self.retranslateUi(set_time)
        QtCore.QMetaObject.connectSlotsByName(set_time)

    def retranslateUi(self, set_time):
        _translate = QtCore.QCoreApplication.translate
        set_time.setWindowTitle(_translate("set_time", "Dialog"))
        self.job_time.setWhatsThis(_translate("set_time", "<html><head/><body><p>Hier die Zeit für den Auftrag eintragen</p><p><br/></p></body></html>"))
        self.job_time.setDisplayFormat(_translate("set_time", "HH:mm:ss"))
        self.start_button.setText(_translate("set_time", "Start"))
        self.pause_button.setText(_translate("set_time", "Pause"))
        self.finish_button.setText(_translate("set_time", "Fertig"))
        self.label.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:14pt; font-weight:600;\">Vorgabe Zeit:</span></p></body></html>"))
        self.kill_Button.setText(_translate("set_time", "Beenden"))
        self.label_2.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:14pt; font-weight:600;\">Auftragsnummer:</span></p><p><br/></p></body></html>"))
        self.label_3.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Verbleibende Zeit:</span></p></body></html>"))
        self.label_4.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Überzogene Zeit:</span></p></body></html>"))
        self.set_button.setText(_translate("set_time", "Setzen"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    set_time = QtWidgets.QDialog()
    ui = Ui_set_time()
    ui.setupUi(set_time)
    set_time.show()
    sys.exit(app.exec_())

Secondly the Control code:

from PyQt5 import QtWidgets, QtCore, QtGui
import StortiTimer

class ControlLCD(QtWidgets.QWidget):
    def __init__(self, parent = None):
        QtWidgets.QWidget.__init__(self, parent)
        self.ui = StortiTimer.Ui_set_time()
        self.ui.setupUi(self)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updateLCD)
        self.timer.start(1000)


    def updateLCD(self):
        self.currenttime = QtCore.QTime.currentTime()
        self.printtimer = self.currenttime.toString("hh:mm:ss")
        self.update
        self.self.ui.remaining_time.display(self.printtimer)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    c = ControlLCD()
    c.show()
    sys.exit(app.exec_())

I believe I've addressed the key issues in the code below.

I pulled your routines into one overall class to simplify the calls.

Your buttons weren't working because you needed to connect them to helper procedures. For example:

        def onClickStartTimer(self):
            self.timer.start()
...
        self.start_button = QPushButton(set_time)
        self.start_button.clicked.connect(self.onClickStartTimer)

The timer start and stop controls then go into the appropriate helper procedures.

I modified updateLCD to decrement your "currenttime" QTime() variable by 1 second every time it is called, and then to display the time. Added test to stop counter after it hit zero. Also updated your Setzen button helper routine to call updateLCD to display your initial time setting.

I added code to make Pause toggle between timer start and stop, depending on the current state of the timer, which is what I assume you wanted.

Hope this helps. See the following:

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

# import StortiTimer
@pyqtSlot() # Enable slots

class Ui_set_time(object):
    

    def onClickStartTimer(self):
        self.timer.start()

    def updateLCD(self): # Subtract 1 sec from currenttime and display it       
        self.currenttime = self.currenttime.addSecs(-1) # Subtract 1 second from currenttime
        self.printtimer = self.currenttime.toString("hh:mm:ss")
        self.remaining_time.display(self.printtimer)

        if self.currenttime == QTime(0,0,0): # Stop after displaying zero seconds.  Place any other needed actions under here when hitting zero.
            self.timer.stop()

    def onClickPauseTimer(self):
        # Toggle between stop and start
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start()

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

    def onClickKill(self):
        # Don't know what goes in here
        pass
        print("In kill button")
        
    def onClickSetTimer(self):
        self.starttime = self.job_time.time()
        self.currenttime = self.starttime.addSecs(1) # Account for updateLCD subtracting 1
        self.updateLCD() # Subtract 1 sec from currenttime and display this initial time       
    
    def setupUi(self, set_time):
        self.timer = QTimer()
        self.timer.setInterval(1000) # Fire every 1000 ms
        self.timer.timeout.connect(self.updateLCD)
        
        set_time.setObjectName("set_time")
        set_time.resize(1125, 824)

        self.job_time = QTimeEdit(set_time)
        self.job_time.setGeometry(QRect(10, 770, 231, 41))
        self.job_time.setStyleSheet("font: 75 16pt \"MS Shell Dlg 2\";")
        self.job_time.setTime(QTime(0, 0, 0))
        self.job_time.setObjectName("job_time")
        
        self.start_button = QPushButton(set_time)
        self.start_button.setGeometry(QRect(960, 40, 150, 90))
        self.start_button.setStyleSheet("font: 75 22pt \"MS Shell Dlg 2\";\n"
"background-color: rgb(0, 255, 0);")
        self.start_button.setAutoDefault(False)
        self.start_button.setDefault(False)
        self.start_button.setFlat(False)
        self.start_button.setObjectName("start_button")
        self.start_button.clicked.connect(self.onClickStartTimer)
        
        self.pause_button = QPushButton(set_time)
        self.pause_button.setGeometry(QRect(960, 140, 150, 90))
        self.pause_button.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"font: 75 22pt \"MS Shell Dlg 2\";")
        self.pause_button.setObjectName("pause_button")
        self.pause_button.clicked.connect(self.onClickPauseTimer)

        self.finish_button = QPushButton(set_time)
        self.finish_button.setGeometry(QRect(960, 240, 150, 90))
        self.finish_button.setStyleSheet("background-color: rgb(0, 85, 255);\n"
"font: 75 22pt \"MS Shell  2\";\n"
"color: rgb(255, 255, 255);")
        self.finish_button.setObjectName("finish_button")
        self.finish_button.clicked.connect(self.onClickFinish)

        self.label = QLabel(set_time)
        self.label.setGeometry(QRect(10, 720, 231, 40))
        self.label.setFrameShape(QFrame.StyledPanel)
        self.label.setFrameShadow(QFrame.Sunken)
        self.label.setLineWidth(10)
        self.label.setMidLineWidth(10)
        self.label.setObjectName("label")

        self.kill_Button = QPushButton(set_time)
        self.kill_Button.setGeometry(QRect(960, 720, 150, 90))
        self.kill_Button.setObjectName("kill_Button")
        self.kill_Button.clicked.connect(self.onClickKill)

        self.label_2 = QLabel(set_time)
        self.label_2.setGeometry(QRect(260, 720, 261, 40))
        self.label_2.setFrameShape(QFrame.StyledPanel)
        self.label_2.setFrameShadow(QFrame.Sunken)
        self.label_2.setLineWidth(10)
        self.label_2.setMidLineWidth(10)
        self.label_2.setObjectName("label_2")

        self.lineEdit = QLineEdit(set_time)
        self.lineEdit.setGeometry(QRect(260, 770, 261, 41))
        self.lineEdit.setObjectName("lineEdit")

        self.remaining_time = QLCDNumber(set_time)
        self.remaining_time.setGeometry(QRect(0, 50, 911, 221))
        self.remaining_time.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(0, 255, 0);")
        self.remaining_time.setDigitCount(8)
        self.remaining_time.setSegmentStyle(QLCDNumber.Flat)
        self.remaining_time.setObjectName("remaining_time")

        self.label_3 = QLabel(set_time)
        self.label_3.setGeometry(QRect(0, 10, 911, 40))
        self.label_3.setFrameShape(QFrame.StyledPanel)
        self.label_3.setFrameShadow(QFrame.Sunken)
        self.label_3.setLineWidth(10)
        self.label_3.setMidLineWidth(10)
        self.label_3.setObjectName("label_3")

        self.extra_time = QLCDNumber(set_time)
        self.extra_time.setGeometry(QRect(0, 320, 911, 221))
        self.extra_time.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 0, 0);")
        self.extra_time.setDigitCount(8)
        self.extra_time.setSegmentStyle(QLCDNumber.Flat)
        self.extra_time.setObjectName("extra_time")

        self.label_4 = QLabel(set_time)
        self.label_4.setGeometry(QRect(0, 280, 911, 40))
        self.label_4.setFrameShape(QFrame.StyledPanel)
        self.label_4.setFrameShadow(QFrame.Sunken)
        self.label_4.setLineWidth(10)
        self.label_4.setMidLineWidth(10)
        self.label_4.setObjectName("label_4")

        self.set_button = QPushButton(set_time)
        self.set_button.setGeometry(QRect(530, 720, 150, 90))
        self.set_button.setStyleSheet("background-color: rgb(255, 170, 0);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
        self.set_button.setObjectName("set_button")
        self.set_button.clicked.connect(self.onClickSetTimer)

        self.retranslateUi(set_time)
        # QMetaObject.connectSlotsByName(set_time)

    def retranslateUi(self, set_time):
        _translate = QCoreApplication.translate
        set_time.setWindowTitle(_translate("set_time", "Dialog"))
        self.job_time.setWhatsThis(_translate("set_time", "<html><head/><body><p>Hier die Zeit für den Auftrag eintragen</p><p><br/></p></body></html>"))
        self.job_time.setDisplayFormat(_translate("set_time", "HH:mm:ss"))
        self.start_button.setText(_translate("set_time", "Start"))
        self.pause_button.setText(_translate("set_time", "Pause"))
        self.finish_button.setText(_translate("set_time", "Fertig"))
        self.label.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:14pt; font-weight:600;\">Vorgabe Zeit:</span></p></body></html>"))
        self.kill_Button.setText(_translate("set_time", "Beenden"))

        self.label_2.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:14pt; font-weight:600;\">Auftragsnummer:</span></p><p><br/></p></body></html>"))
        self.label_3.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Verbleibende Zeit:</span></p></body></html>"))
        self.label_4.setText(_translate("set_time", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Überzogene Zeit:</span></p></body></html>"))

        self.set_button.setText(_translate("set_time", "Setzen"))


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    set_time = QDialog()
    ui = Ui_set_time()
    ui.setupUi(set_time)
    set_time.show()
    sys.exit(app.exec_())


#Secondly the Control code:


# class ControlLCD(QWidget):
#     def __init__(self, parent = None):
#         QWidget.__init__(self, parent)
#         # self.ui = StortiTimer.Ui_set_time()
#         self.ui.setupUi(self)


#     # def updateLCD(self):
#     #     self.currenttime = QTime.currentTime()
#     #     self.printtimer = self.currenttime.toString("hh:mm:ss")
#     #     self.update
#     #     self.ui.remaining_time.display(self.printtimer)


# if __name__ == "__main__":
#     import sys
#     app = QApplication(sys.argv)
#     c = ControlLCD()
#     c.show()
#     sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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