Tkinter GUI freezing, used Thread then encountered RuntimeError: threads can only be started once
Cameron Simpson
cs at cskk.id.au
Tue Jan 10 19:13:42 EST 2023
On 10Jan2023 18:32, MRAB <python at mrabarnett.plus.com> wrote:
>I don't like how you're passing Thread...start as an argument. IMHO, it
>would be better/cleaner to pass a plain function, even if the only
>thing that function does is to start the thread.
Yes, and this is likely the thing causing the cited exception "threads
can only be started once". Your setup of the button with the action
defined as:
Thread(....).start
creates a _single_ new Thread _when you define the button_, and makes
hte button callback try to start it. On the second and following
callback, you're trying to start the _same_ single Thread again.
Do as MRAB suggests and have the callback create-and-start a Thread
instead of just starting an _existing_ Thread.
Also, for simple quick things there's no need to use a Thread at all. If
the final version of the programme is going to do something long running
at that point, then sure.
>I can't tell what 'change_flag' is doing because of the formatting
>issue. Is it doing GUI stuff? In a thread? If yes, don't do that. The
>GUI doesn't like that. Only the main thread should do GUI stuff.
Aye. This is very important in almost all GUI toolkits.
Bit me very badly with Qt once, badly in that the segfaults (yes!
segfaults! in a Python app!) were erratic and very timing dependent,
making them hard to reproduce and understand. It wasn't until I
_realised_ it was thread/concurrency related that I could fix it.
Note that in Tk you can have a callback do GUI work, just not in a
separate thread.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list