GUI freezing while updating (result from QRunner)

Heads up! You've already completed this tutorial.

Jeremie | 2020-05-11 07:03:06 UTC | #1

Thanks for sharing this very informative tutorial.

I was wondering, what do you do when the GUI updating part is what makes the GUI freeze. For example if you add a time.sleep(0.8) in recurring_timer() then the GUI becomes unresponsive for 0.8 second. Which means you cannot click on the Danger button for that period of time for example (and you might think that you clicked the button but the click was never registered)

I am experiencing this issue right now as I am trying to make a live stock market GUI with 100s of prices updating live. In my case the main thread cannot cope with displaying all the prices (even throttled to only once a second) and still allow the user to type GUI inputs (like in a QLineEdit for example, or clicking some buttons). I have tried to run parallel threads to update the GUI but as many have pointed out on the internet this triggers random inexplicable crashes.

Any suggestions will be most welcome.

Thanks again

martin | 2020-05-11 07:03:57 UTC | #2

Hi Jeremie. glad you found the tutorial helpful!

Re: your question when doing anything that is on the main thread (and can potentially interrupt the GUI) it's important to remember to prefer doing things often, than doing them for a long time. It's fine to interrupt the GUI 100x per second, as long as those interrupts are short. When doing this Qt's own events will be interleaved with yours, and the delay will be imperceptible. Of course there is a point where the overhead of multiple events outweights the benefits, but this is higher than you might expect.

In your case I would try to either second the prices as individual "update" events, or batched at say 5 or so (experiment a bit). This should keep the delays below perceptible level, and allow you to update more often.

A more general point, when updating data "live" you almost always want some kind of (very short term) buffer than allows you to throw away data if you're not able to use it before the next data appears. You won't want to do this for graph plots, etc. but it can be helpful for simple numeric displays where out of data isn't shown.

Hope this helps!

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

GUI freezing while updating (result from QRunner) was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.