Tkinter.Canvas thread safety problem?

Diez B. Roggisch deets at nospam.web.de
Tue Jul 7 09:23:34 EDT 2009


Zdenek Maxa wrote:

> Hello,
> 
> I have started a project using Tkinter. The application performs some
> regular checks in a thread and updates Canvas components. I have
> observed that sometimes the application hangs when it is about to call
> canvas.itemconfig() when the thread is about to terminate in the next
> loop.
> 
> Experimenting with this problem for a while, I have compiled a little
> example which always reproduces the problem. Commenting out the line 52
> (before canvas.itemconfig()), the example always finishes all right,
> having the delay there, it hangs.
> 
> I would like to ask if you could have a look at the snippet in the
> attachment and tell me if that is actually me doing something wrong or
> indeed Tkinter thread safety problem and what the workaround could be.
> 
> Could you please also comment on wxPython thread safety?
> 

As a rule of thumb, GUI-toolkits aren't threadsafe. Qt being a notable
exception to this rule.

There are various ways to cope with this, mostly through injecting events
into the event-queue of the main gui thread, or using timers.

This is also true for wx (google wx python threading for a plethora of
information on this)

For Tx, I dimly remember an "after" function that can be used to invoke
timer-based code. Use that to update aggregated data from the events.

Diez



More information about the Python-list mailing list