Tkinter: is event_generate safe from background thread?

David LeBlanc whisper at oz.net
Fri Dec 26 19:30:43 EST 2003


> In general, I thought one should not mess with Tkinter from a background
> thread. But Eric Brunel recently posted a clever suggestion that one
> could communicate from a background thread to the main thread using
> event_generate to create custom events. Very neat: it's direct and it
> saves polling. He said it works for him on Linux, Windows and Solaris.
> I'd love to use it in my code, but am wondering if it's truly safe, e.g.
> safe by design and likely to remain so?
>
> Also, are there any other things one can safely do with Tkinter from
> background threads (such as set variables)?
>
> -- Russell

The general rule is that the thread that owns the GUI can be the only one
that can directly manipulate the GUI (call it's functions). Sending messages
that the GUI thread processes is nearly always just fine since the GUI
thread, and it's state, are used to do the actual GUI manipulation.

Directly setting variables from one thread to any other thread without any
sort of access control (sempaphore, etc.) is a very not good thing to do,
whether it's a GUI thread or not!

HTH,

Dave LeBlanc
Seattle, WA USA






More information about the Python-list mailing list