Tkinter.Canvas thread safety problem?

Z

Zdenek Maxa

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?

I am using:

Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
[GCC 4.3.3]

2.6.26-1-amd64 #1 SMP Fri Mar 13 19:34:38 UTC 2009 x86_64 GNU/Linux

Thanks in advance,
Zdenek
 
D

Diez B. Roggisch

Zdenek said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top