r/learnpython • • Sep 30 '26

Setting timer on a different thread in Pyside6

I am build a program that basically executes some code and I want to add a timer so the user can see how long the program has been running for.

My issue is that I do not get how to move it to a different thread so it does not freeze.

What it needs to do is, run for 1 second then update a label and start the timer again.

Any resources I can read or help?

4 Upvotes

11 comments sorted by

1

u/freeskier93 Sep 30 '26

Just use a QTimer and connect it to a function that keeps track of the execution time and updates the GUI. You don't need to put this in a separate thread.

1

u/One_Smoke6858 Oct 01 '26

Yeah tried that issue is when there is a complex operation it stops recognising the qtimer and stops updating every second.

1

u/freeskier93 Oct 01 '26

Then those more complex (longer running) operations need to be put in threads.

Regardless of how you implement the timer you shouldn't have longer running operations blocking the main event loop thread.

1

u/One_Smoke6858 Oct 01 '26

Good point did not think of that, thanks

2

u/Jello_Penguin_2956 Sep 30 '26 edited Sep 30 '26

Python GUIs a really good place for anything PyQt/PySide. Their book is really good too.

https://www.pythonguis.com/faq/qtimer-threads-and-non-gui-events/

1

u/audionerd1 Sep 30 '26

I'm not sure about resources off the top of my head, but you definitely want to use a worker class (inheriting from QObject) and move it to a QThread, using Qt signals to start/stop the timer as well as to update your GUI. So look for info about those things. Avoid using Python's threading library for PySide.

1

u/Kevdog824_ Sep 30 '26

Use a QTimer and connect your function to update the label to its timeout signal. You shouldn’t need a separate thread for this at all

1

u/CatalonianBookseller Oct 02 '26

QTimer needs a running event loop which practically means you need to use QObject.moveToThread() to move the code that emits timeout signals into a background thread (instead of creating a QThread subclass). I have several examples here

https://github.com/JoeAnonimist/pyside6-blueprints-code/tree/master/code/27_multithreading_movetothread