Threaded Tkinter

Gerald Gutierrez gutier at
Thu Jun 3 02:53:01 EDT 1999

Hi all.

I have two questions and I'd really appreciate it if someone could
help me sort it out.

My first one -- I'm starting out with Tkinter. I wrote the code below,
which will supposedly create a button with an image, that image
changing every second. If I set the button to call swapIcons() when
pressed, the image changes fine, but using the SpinThread, the icons
don't change. Is this because Tkinter is not thread-safe and I cannot
safely change the button's image property from within a thread other
than the main one?

import Tkinter

class App(Tkinter.Frame):

    def __init__(self):

	self.im1 = Tkinter.Image("photo", "tux", {"file":"oldtux.gif"})
	self.im2 = Tkinter.Image("photo", "smiley", {"file":"smiley.gif"})
	self.curImage = "tux"
	self.b = Tkinter.Button(self)
	self.b["image"] = self.curImage
	self.b["command"] = self.swapIcons

    def swapIcons(self):
	if (self.curImage == "tux"):
	    self.curImage = "smiley"
	elif (self.curImage == "smiley"):
	    self.curImage = "tux"

	self.b["image"] = self.curImage

import threading
import time
class SpinThread(threading.Thread):

    def __init__(self, app):
	threading.Thread.__init__(self) = app

    def run(self):
	while (1):
app = App()
st = SpinThread(app)

My second question -- I'd like to use Tkinter an Fnorb (CORBA)
together as the UI and the network agent, respectively. Incoming CORBA
calls will change appliation state and cause a change in the UI.

Now, since both Tkinter and Fnorb have their own event loops, I
probably have to run them each in a separate thread. This is fine with
me. The problem is that, if my first problem above is caused by the
fact that I cannot use Tkinter in mutiple threads, I will need to send
UI change requests for processing from within the main thread. 

One way I know to do this is to have some sort of queue to which I can
send messages. Tkinter's event loop will have to process the
queue. How do I go about accomplishing this?

If this is not the correct way, what is?

Thanks for any help.

Please forward replies to gutier at

More information about the Python-list mailing list