Mixing Txinter and Pygame

Tim Knauf tim at upperorbit.com
Mon Feb 21 19:17:37 EST 2005


Hi everyone, I'm glad to have found this list.

I've written a small script for my own use which, amongst other things, 
captures mouse click information from a window containing an image. I 
used Pygame to manage the image window, as it was the easiest way to 
implement the functionality I needed. The surrounding interface windows 
(there are two) are constructed with Tkinter.

Despite their unholy union, Pygame and Tkinter seem, generally, to 
cooperate. I'm using this main loop to update one, then the other:

while 1:
    gameLoop() # This function pumps the Pygame events and checks for 
mouse and keyboard events
    pygame.time.wait(10)
    mainwin.update() # mainwin is an instance of Application class, 
which is a child of Tkinter.frame

I have my interface set up so that when *any* of the windows' close 
boxes are clicked, this function will be called:

# This portion of the Pygame loop calls doquit() when it gets a 'QUIT' 
event...
def gameLoop():
    pygame.event.pump()
    for event in pygame.event.get():
        if event.type == QUIT:
            doquit()
            # Etc.

    # And this portion of the Tkinter interface sets the 
WM_DELETE_WINDOW protocol to call doquit()
    def createWidgets(self):
        self.title("Region Management")
        self.geometry('+830+8')
        self.protocol("WM_DELETE_WINDOW", doquit)
        # Etc.

# A temporary file is removed, and both Pygame and Tkinter are 
instructed to quit
def doquit():
    if os.access('recalc.tmp', os.F_OK):
        os.remove('recalc.tmp')
    pygame.quit()
    mainwin.master.destroy()

Perhaps you've begun to see where I might be having problems. You see, 
if I close the script by closing the Pygame window, I get this exception:

Traceback (most recent call last):
  File "D:\Development\Python\sludge helpers\addScreenRegion 
helper.pyw", line 363, in ?
    mainwin.update()
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 859, in update
    self.tk.call('update')
TclError: can't invoke "update" command:  application has been destroyed

Conversely, if I close the application by closing a Tkinter window, I 
get this exception:

Traceback (most recent call last):
  File "D:\Development\Python\sludge helpers\addScreenRegion 
helper.pyw", line 361, in ?
    gameLoop()
  File "D:\Development\Python\sludge helpers\addScreenRegion 
helper.pyw", line 203, in gameLoop
    pygame.event.pump()
error: video system not initialized

Obviously, Pygame doesn't like Tkinter telling it to quit (when it's 
trying to do something from its internal loop) and vice versa. Is there 
a simple way that I can avoid getting these exceptions on exit, or have 
I taken the wrong approach? Everything else appears to work fine. Please 
do excuse me if this seems a silly question, as I'm fairly new to Python 
and Pygame, and a total novice when it comes to Tkinter.

On a side note, has anyone else found the Tkinter documentation awfully 
obscure? I've found Python a joy to learn about, and Pygame's tutorials 
are a lot of fun. I can't say the same for Tkinter, and found myself 
having to do many Google searches before I uncovered information I could 
put to use. Has anyone found any high-quality (online) documentation 
that proves me wrong? :^)

Thanks in advance,
Tim Knauf



More information about the Python-list mailing list