[Tkinter-discuss] Tkinter and threading
Russell E. Owen
rowen at cesmail.net
Mon May 9 21:04:34 CEST 2005
In article <ifz1al.3ldie0 at www.pandorasdream.com>,
Vania Smrkovski <vania at pandorasdream.com> wrote:
> Hello,
> New to this group, and I have not found much in the way of documentation
> for advanced uses of Tkinter, yet. So far, Tk3k is the most advanced
> resource I have found yet.
> I am trying to implement threading in my Tkinter app. I have managed to
> get a button to fire an FTP download into one of my text areas, but to get
> rid of the delay on the GUI while the FTP process was being handled, I
> created a thread in the button\'s command handler....
You cannot safely make Tkinter from any thread other than the one that
started the tkinter mainloop (i.e. your main thread unless you are doing
something really weird).
As far as network communication goes, you have several options...
- Use the free "twisted framework". It integrates with Tkinter and other
GUI toolkits, handles most of the standard protocols and is reported to
be very good.
- For ftp, do the transfer in the background and poll the state. This is
especially appropriate if you have multiple transfers since one polling
loop can poll all of them. For an example see RO.Comm.FTPGet and the
associated widget RO.Wdg.FTPLogWdg in the RO package
<http://www.astro.washington.edu/rowen/ROPython.html>.
- For TCP/IP use a tcl socket and use file events to handle incoming
data and errors. This has the advantage of being asynchronous (and is
completely cross platform unlike the a similar technique at the Tkinter
level). For an example see RO.Comm.TCPConnection and the underlying
RO.Comm.TkSocket.
-- Russell
P.S. there were rumors a few years ago that you could safely generate
Tkinter events from a background thread and have the main thread handle
them. However, I am pretty sure (based on my own tests) that it's not
actually safe to do that. My suggestion is to NEVER deal with Tkinter or
Tk from a background thread.
More information about the Tkinter-discuss
mailing list