Search code examples
python-3.xuser-interfacepyqt5qlcdnumber

Reset Output on QLCD Number when Timer Resets


I currently have a code, which starts a countdown timer via a pushbutton, What i would like is whenever the user presses the button, the timer to re start from 120, currently it just continues to run and doesn't reset.

relevant section of My code is as follows:

class ControlMainWindow(QtWidgets.QMainWindow):
    def __init__(self,parent=None):
        super(ControlMainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.OuterSwitch.clicked.connect(self.OuterControl)
        self.ui.InnerSwitch.clicked.connect(self.InnerControl)

        self.current_timer = None
        self.LCD_timer = None
        self.i = 121

    def LCDtimer(self):
        if self.LCD_timer:
            self.LCD_timer.stop()
            self.LCD_timer.deleteLater()
        self.LCD_timer = QtCore.QTimer()
        self.LCD_timer.start(0)
        self.LCD_timer.timeout.connect(self.updateLCDNumber)

    def starttimer(self):
        if self.current_timer:
            self.current_timer.stop()
            self.current_timer.deleteLater()
        self.current_timer=QtCore.QTimer()
        self.current_timer.setSingleShot(True)
        self.current_timer.start(120000)
        self.current_timer.timeout.connect(self.InnerSwitchOn)


    def updateLCDNumber(self):
        if self.i !=0:
            QtTest.QTest.qWait(1000)
            self.i = self.i-1
            self.ui.Timer.display(self.i)

    def InnerSwitchOn(self):   
        self.ui.InnerSwitch.setEnabled(True)

    def OuterControl(self):
        if GPIO.input(Inner)==0: #InnerDoorOpen
            return GPIO.output(Outer,GPIO.HIGH),print("Please Close Inner Door")
        elif GPIO.input(Outer) ==1: #Outer Door Close
            self.ui.OuterSwitch.setText(QtWidgets.QApplication.translate("MainWindow", "Close Outer Door",None))
            QtWidgets.QApplication.processEvents()
            return GPIO.output(Outer,GPIO.LOW) #Open Outer Door
        elif GPIO.input(Outer) == 0: #OuterDoor Open
            self.ui.InnerSwitch.setEnabled(False)
            self.ui.OuterSwitch.setText(QtWidgets.QApplication.translate("MainWindow", "Open Outer Door",None))
            QtWidgets.QApplication.processEvents()
            self.LCDtimer()
            self.starttimer()
            return GPIO.output(Outer,GPIO.HIGH) #Close OuterDoor

How Can I get the timer to reset whenever the push button is pressed again instead of it just continously running? Thanks.


Solution

  • So After 2 Days I managed to solve this. Only One line of code was missing really.

    To Solve this it was simple, I needed to set the LCD display to 120 prior to the function being carried out.

    to give an example:

    --Calling the PushButton to open and close Outer Door:

        def OuterControl(self):
            if GPIO.input(Inner) == 0:  # InnerDoorOpen
                return GPIO.output(Outer, GPIO.HIGH), print("Please Close Inner Door")
            elif GPIO.input(Outer) == 1:  # Outer Door Close
                self.ui.OuterSwitch.setText(QtWidgets.QApplication.translate("MainWindow", "Close Outer Door", None))
                QtWidgets.QApplication.processEvents()
                return GPIO.output(Outer, GPIO.LOW)  # Open Outer Door
            elif GPIO.input(Outer) == 0:  # OuterDoor Open
                self.ui.InnerSwitch.setEnabled(False)
                self.ui.OuterSwitch.setText(QtWidgets.QApplication.translate("MainWindow", "Open Outer Door", None))
                QtWidgets.QApplication.processEvents()
                self.LCDtimer()
                self.starttimer()
    return GPIO.output(Outer, GPIO.HIGH)
    

    Then setting up a timer and syncing the LCD Number with the timer:

            self.current_timer = None
            self.LCD_timer = None
            self.i = 121
    
    
        def LCDtimer(self):
            if self.LCD_timer:
                self.LCD_timer.stop()
                self.LCD_timer.deleteLater()
            self.LCD_timer = QtCore.QTimer()
            self.ui.Timer.display(120)
            self.LCD_timer.start(0)
            self.LCD_timer.timeout.connect(self.updateLCDNumber)
    
        def starttimer(self):
            if self.current_timer:
                self.current_timer.stop()
                self.current_timer.deleteLater()
            self.i = 121
    
            self.current_timer = QtCore.QTimer()
            self.current_timer.setSingleShot(True)
            self.current_timer.start(123000)
            self.current_timer.timeout.connect(self.InnerSwitchOn)
    
        def updateLCDNumber(self):
            if self.i != 0:
                QtTest.QTest.qWait(1000)
                self.i -= 1
                self.ui.Timer.display(self.i)
    
        def InnerSwitchOn(self):
            self.ui.InnerSwitch.setEnabled(True)