VIdeo Converence Software: Tk and thread synchronization

Eric Brunel eric_brunel at
Tue Oct 3 09:39:09 CEST 2006

On Mon, 02 Oct 2006 14:42:15 +0200, Paolo Pantaleo  
<paolopantaleo at> wrote:
> Hi,
> I am going on writing my video conference software. I wrote the video
> grab, code/decode, and netwoark (multicast) transport.
> I have one thread doing this:
> [thread 1]
> while True:
>   for some times:
>     my_socket.recv() #blocking here
>     store data
>   compute image #here we have a complete new image to display
> Now, I was thinking to display the image in a  Tk window. But I think
> i will need a separate thread to run the mainloop() [thread 2], right?

I don't know if tk likes to be run in anything else than the main thread;  
never tested it. If you can use the main thread for tk, you'll probably  
have far less problems.

>  How can I signale to the Tk thread that a new image is ready to be
> shown? I was thinkin on using an event generated for the first
> (network) thread. But I don't know how to do it exactly. Any
> suggestion, please?

Quite simple: in the tk thread, do:

widget.bind('<<MyEvent>>', command)

Then in the secondary thread, do:

widget.event_generate('<<MyEvent>>', when='tail')

'<<MyEvent>>' can be anything, as long as you surround it with << & >>  
(these are custom events in tk). Don't forget to put the when='tail' in  
the event_generate call, or your binding may get called immediatly in the  
current thread.

> What if I access to Tk object form thread 1, is Tk thread safe?

General answer: no, it isn't. The only thing that seems to work everytime  
on tk widgets from secondary threads is generating events as above. If you  
try to do anything else, you may experience deadlocks or even crashes.

python -c "print ''.join([chr(154 - ord(c)) for c in  

More information about the Python-list mailing list