我目前有一个代码,它通过按钮启动倒数计时器,我想要的是每当用户按下按钮时,计时器从 120 重新启动,目前它只是继续运行并且不会重置。
我的代码的相关部分如下:
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
如何让计时器在再次按下按钮时重置,而不是连续运行? 谢谢。
所以两天后我设法解决了这个问题。实际上只缺少一行代码。
为了解决这个问题,这很简单,我需要在执行该功能之前将LCD显示屏设置为120。
举个例子:
--调用按钮打开和关闭外门:
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)
然后设置定时器并将液晶屏号码与定时器同步:
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)