我正在使用它PYQT5来构建 GUI,并使用它APScheduler来管理我想要运行的作业。我将调度程序项目和计时器项目分解为自己的类,然后将它们连接到主文件中。
我遇到的问题是,一旦计时器完成一个周期,我会尝试为课程添加时间Timer,并在调度程序再次运行之前重新开始下一次倒计时。我收到两个错误或警告,但我不知道如何修复它们。他们是:
QObject::killTimer: Timers cannot be stopped from another thread
QObject::startTimer: Timers cannot be started from another thread
一旦抛出这些,GUI 就会更新,但不再倒计时。我将附上我发现的可以重现错误的最简单版本。非常感谢任何帮助,并感谢您的宝贵时间。
Main.py
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from Timer import Timer
from Schedule import Scheduler
import datetime
class MyMainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
vbox = QtWidgets.QVBoxLayout()
central_widget.setLayout(vbox)
self.start_pushButton = QtWidgets.QPushButton()
self.start_pushButton.setText("Start")
self.start_pushButton.clicked.connect(self.start_schedule)
vbox.addWidget(self.start_pushButton)
self.pages_qsw = QtWidgets.QStackedWidget()
vbox.addWidget(self.pages_qsw)
self.time_passed_qll = QtWidgets.QLabel()
vbox.addWidget(self.time_passed_qll)
self.my_timer = Timer()
self.my_timer.get_seconds.connect(self.update_gui)
self.sch = Scheduler()
def start_schedule(self):
self.sch.add(self.hello)
self.sch.start()
self.start_my_timer()
def start_my_timer(self):
next_run = self.sch.next_occurance().replace(tzinfo=None) # This removes the time zone.
a = datetime.datetime.now()
difference = next_run - a
self.my_timer.addSecs(difference.seconds)
self.my_timer.timer_start()
def hello(self):
print("hello world")
self.start_my_timer()
@QtCore.pyqtSlot(str)
def update_gui(self,seconds):
self.time_passed_qll.setText(str(seconds))
app = QtWidgets.QApplication(sys.argv)
main_window = MyMainWindow()
main_window.show()
sys.exit(app.exec_())
繁花如伊
相关分类