I'm running a QWidget in python using an extended version of the following code:
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.timer = QTimer(self)
self.timer.timeout.connect(self.hello_world)
self.timer.start(10000)
def hello_world(self):
print("Hello world")
class MyMainWindow(QMainWindow):
def __init__(self, gates, **kwargs):
super().__init__()
self.widget = MyWidget()
self.setCentralWidget(self.widget)
def _run_app(stop_event, widget_queue):
_toggle_lock()
app = QApplication(sys.argv)
main_widget = InteractiveWindow()
main_widget.show()
widget_queue.put(main_widget.widget)
print("GUI running")
app.aboutToQuit.connect(_toggle_lock)
while not stop_event.is_set():
app.processEvents()
app.quit()
_toggle_lock(False)
def _toggle_lock(state=None):
global LOCK
if state is None:
LOCK = not LOCK
else:
LOCK = state
def open_gui():
if not LOCK:
stop_event = threading.Event()
widget_queue = queue.Queue()
gui_thread = threading.Thread(target=_run_app, args=(stop_event, widget_queue), daemon=True)
gui_thread.start()
return gui_thread, stop_event, widget_queue.get()
else:
print("GUI is already running")
def close_gui(gui_thread, stop_event):
if gui_thread.is_alive():
stop_event.set()
gui_thread.join()
Once I start up the widget from a python script using thread, stop, widget = open_gui(), the timer runs as expected printing "Hello world" every 10 seconds. I can also stop it from the notebook using widget.timer.stop() and it will stop printing. The problem comes when I try to restart the timer by calling widget.timer.start(10000), nothing happens.
I would like to find a way to restart the timer from outside its thread.
timeout). If you run the code in a terminal or prompt you should be able to see warnings when trying to call bothstop()andstart(). So, even ifstop()seems to work, it's inappropriate to use it from external threads just like it's not possible to callstart(), as the QTimer documentation clearly explains. In general, the UI thread should always be the main one, doing the opposite (and trying to work around it) is almost always due to poor design.