[Tkinter-discuss] Tkinter and threading

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu May 5 08:35:04 CEST 2005

Vania Smrkovski 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.  
>   On my Linux box, the below handler works as I expected.  If I do not have the host, username and password defined, I get a showerror() pop telling the user of the problem.  
>   However, on my PC, I get a beep, but no pop.  
>   The problem appears to be with calling a Tkinter method from within a thread.  When I pop a showerror() command outside of the thread, I get exactly what I ask for.  But, as you can see in the exception handler, my PC gets the PRINT statement, but no error dialog.
> Here is a snippet from one of the class methods:
> def HandleFTP(self):
> 	def AppendLine(data):
> 		self.UI.T.insert(END, data + \"\\n\")
> 	def threadcode():
> 		try:
> 			ftp = ftplib.FTP(self.UI.Host)
> 			ftp.login(self.UI.Username,self.UI.Password)
> 			ftp.cwd(\'/www/\')
> 			filename = \'admin.php\'
> 			ftp.retrbinary(\'RETR \' +filename, AppendLine)
> 			#ftp.storbinary(\'STOR \' +filename, open(filename, \'rb\'))
> 		except AttributeError:
> 			print \"Missing Configuration\", \"To use FTP, add the following to \" + \\
> 			\"your config.ini file\\n\\n\" + \\
> 			\"[FTP]\\n\" + \\
> 			\"Host=ftp.wherever.com\\n\" + \\
> 			\"Username=[yourusername]\\n\" + \\
> 			\"Password=[yourpassword]\"
> 			showerror(\"Missing Configuration\", \"To use FTP, add the following to \" + \\
> 			\"your config.ini file\\n\\n\" + \\
> 			\"[FTP]\\n\" + \\
> 			\"Host=ftp.wherever.com\\n\" + \\
> 			\"Username=[yourusername]\\n\" + \\
> 			\"Password=[yourpassword]\")
> 	t = threading.Thread(target=threadcode, name=\"ftpthread\")
> 	t.setDaemon(1)
> 	t.start()
>   Am I missing something, or is there some specific approach required to do threading?  In all other ways, the theading works exactly as expected.  That is, when I provide the host, username and password, I get my button and UI fully functional with no delays, and then the text field gets populated.  
> Thanks,
> _____________________
> Vania Smrkovski
> www.pandorasdream.com


You should not make any calls to update Tkinter widgets from another
thread, as you've seen for some platforms it seems to work... some of 
the time but not all (or all platforms!)  I think there are some really 
good examples of threading and Tkinter in the Python Cookbook

A quick google for "python Tkinter threading"  throws this out:


I'm sure there are more


More information about the Tkinter-discuss mailing list