from PySide6.QtCore import QObject
class Motors(QObject):
def __init__(self,start:bool=None,stop:bool=None,*args,**kwargs):
super().__init__(*args,**kwargs)
self.start = start
self.stop = stop
self.time_stop_id = None
self.time_start_id = None
def run(self):
if self.start and not self.stop:
if self.time_stop_id is not None :
self.killTimer(self.time_stop_id)
self.time_start_id = self.startTimer(100)
if self.stop and not self.start:
if self.time_start_id is not None:
self.killTimer(self.time_start_id)
self.time_stop_id = self.startTimer(100)
def timerEvent(self, event):
if event.timerId()==self.time_start_id:
print("开始定时器中事件")
print(event.timerId())
if event.timerId() == self.time_stop_id:
print("停止定时器中事件")
print(event.timerId())
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class Ui(QWidget):
def __init__(self):
super().__init__()
self.resize(600,600)
self.btn = QPushButton("btn1",self)
self.btn2 = QPushButton("btn2",self)
self.btn.move(100,50)
self.btn2.move(100,150)
self.btn.clicked.connect(self.btn_click)
self.btn2.clicked.connect(self.btn_click2)
self.motor = Motors()
def btn_click(self):
self.motor.start = 1
self.motor.stop = 0
self.motor.run()
def btn_click2(self):
self.motor.start = 0
self.motor.stop = 1
self.motor.run()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Ui()
win.show()
sys.exit(app.exec())
上面的定时器两个都会执行,停止不了。
`Motors`类中的`run`方法逻辑存在一些问题,导致定时器两个都执行的原因是因为在`run`方法中,即使关闭了一个`timeId`,但是在下一次执行`run`方法时又会重新开启一个新的`timeId`,导致两个定时器都在运行。 为了解决这个问题,可以在`run`方法中增加判断,只有当`timeId`不存在时才开启新的定时器。同时,在关闭定时器时,将`timeId`置为`None`,以确保只有一个定时器在运行。 以下是修改后的`Motors`类的代码片段:
from PySide6.QtCore import QObject
class Motors(QObject):
def __init__(self,start:bool=None,stop:bool=None,*args,**kwargs):
super().__init__(*args,**kwargs)
self.start = start
self.stop = stop
self.time_stop_id = None
self.time_start_id = None
def run(self):
if self.start and not self.stop:
if self.time_stop_id is not None:
self.killTimer(self.time_stop_id)
self.time_stop_id = None
if self.time_start_id is None:
self.time_start_id = self.startTimer(1000)
if self.stop and not self.start:
if self.time_start_id is not None:
self.killTimer(self.time_start_id)
self.time_start_id = None
if self.time_stop_id is None:
self.time_stop_id = self.startTimer(1000)
def timerEvent(self, event):
print(event.timerId())
if event.timerId()==self.time_start_id:
print("start id is here")
if event.timerId() == self.time_stop_id:
print("stop id is here")
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class Ui(QWidget):
def __init__(self):
super().__init__()
self.resize(600,600)
self.btn = QPushButton("btn1",self)
self.btn2 = QPushButton("btn2",self)
self.btn.move(100,50)
self.btn2.move(100,150)
self.btn.clicked.connect(self.btn_click)
self.btn2.clicked.connect(self.btn_click2)
self.motor = Motors()
def btn_click(self):
self.motor.start = 1
self.motor.stop = 0
self.motor.run()
def btn_click2(self):
self.motor.start = 0
self.motor.stop = 1
self.motor.run()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Ui()
win.show()
sys.exit(app.exec())
问题得以解决