[Tkinter-discuss] Tkinter and threading

Vania Smrkovski vania at pandorasdream.com
Wed May 4 17:20:45 CEST 2005

  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():
			ftp = ftplib.FTP(self.UI.Host)
			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\" + \\
			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\" + \\
	t = threading.Thread(target=threadcode, name=\"ftpthread\")

  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.  

Vania Smrkovski

More information about the Tkinter-discuss mailing list