tkinter, sockets and threads together

Martin v. Löwis martin at v.loewis.de
Thu Sep 11 16:43:19 EDT 2003


juliagoolia301 at hotmail.com (Julia Goolia) writes:

> i read that it is bad to use threads with tkinter.  so my question is
> how does one create a gui program with sockets?  at one point you have
> to call mainloop() which does not return.  then you are not free to do
> stuff with the sockets.
> 
> what's up with that?

On Unix, you can use Tk file handlers for sockets, i.e. have Tcl
select(2) not only on the connection to the X11 server (which is a
socket itself), but also select on your sockets. This is readily
supported in _tkinter.

On Windows, you have no choice but to use threads. If the socket
processing threads have no relationship to the Tk thread, everything
is fine. However, in many cases, interaction of some kind is
needed. In this case, it is ok to call Tk methods from other threads,
as long as they translate to "fast" commands, i.e. commands that only
modify state, but don't block - you should not run the mainloop in
multiple threads.

Regards,
Martin





More information about the Python-list mailing list