Mixing Txinter and Pygame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Knauf

    #1

    Mixing Txinter and Pygame

    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.wai t(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.pu mp()
    for event in pygame.event.ge t():
    if event.type == QUIT:
    doquit()
    # Etc.

    # And this portion of the Tkinter interface sets the
    WM_DELETE_WINDO W protocol to call doquit()
    def createWidgets(s elf):
    self.title("Reg ion Management")
    self.geometry(' +830+8')
    self.protocol(" WM_DELETE_WINDO W", doquit)
    # Etc.

    # A temporary file is removed, and both Pygame and Tkinter are
    instructed to quit
    def doquit():
    if os.access('reca lc.tmp', os.F_OK):
    os.remove('reca lc.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\addScre enRegion
    helper.pyw", line 363, in ?
    mainwin.update( )
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 859, in update
    self.tk.call('u pdate')
    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\addScre enRegion
    helper.pyw", line 361, in ?
    gameLoop()
    File "D:\Development \Python\sludge helpers\addScre enRegion
    helper.pyw", line 203, in gameLoop
    pygame.event.pu mp()
    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
  • Eric Brunel

    #2
    Re: Mixing Txinter and Pygame

    On Tue, 22 Feb 2005 13:17:37 +1300, Tim Knauf <tim@upperorbit .com> wrote:[color=blue]
    > 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.wai t(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.pu mp()
    > for event in pygame.event.ge t():
    > if event.type == QUIT:
    > doquit()
    > # Etc.
    >
    > # And this portion of the Tkinter interface sets the
    > WM_DELETE_WINDO W protocol to call doquit()
    > def createWidgets(s elf):
    > self.title("Reg ion Management")
    > self.geometry(' +830+8')
    > self.protocol(" WM_DELETE_WINDO W", doquit)
    > # Etc.
    >
    > # A temporary file is removed, and both Pygame and Tkinter are
    > instructed to quit
    > def doquit():
    > if os.access('reca lc.tmp', os.F_OK):
    > os.remove('reca lc.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\addScre enRegion
    > helper.pyw", line 363, in ?
    > mainwin.update( )
    > File "C:\Python24\li b\lib-tk\Tkinter.py", line 859, in update
    > self.tk.call('u pdate')
    > 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\addScre enRegion
    > helper.pyw", line 361, in ?
    > gameLoop()
    > File "D:\Development \Python\sludge helpers\addScre enRegion
    > helper.pyw", line 203, in gameLoop
    > pygame.event.pu mp()
    > 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.[/color]

    Well, since these are just exceptions, a simple try... except block would be fine, and you can even figure out the reason for the exception. Here is what I'd do:
    - when you create your Tkinter main window, initialize an attribute that you'll use to see if the application has quit, e.g mainwin.hasQuit = False
    - rewrite doquit this way:
    def doquit():
    if os.access('reca lc.tmp', os.F_OK):
    os.remove('reca lc.tmp')
    mainwin.hasQuit = True
    pygame.quit()
    - rewrite your custom event loop this way:
    while 1:
    gameLoop()
    pygame.time.wai t(10)
    try:
    mainwin.update( )
    except TclError:
    if not mainwin.hasQuit :
    raise

    And: (1) your problem should go away; (2) the "real" exceptions you may get from Tkinter windows should not passed unnoticed.
    [color=blue]
    > 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? :^)[/color]

    Incomplete, but very useful: http://www.pythonware.com/library/tk...tion/index.htm
    But the best reference documentation you'll ever find is the tcl/tk man pages, on-line here: http://www.tcl.tk/man/tcl8.4/TkCmd/contents.htm
    It unfortunately requires to know how to convert the tcl/tk syntax to Python/Tkinter syntax, but it is actually quite easy (mainly read "option=val ue" when the tcl/tk documentation says "-option value")

    HTH
    - Eric Brunel -

    Comment

    • Tim Knauf

      #3
      Re: Mixing Txinter and Pygame

      Eric Brunel wrote:
      [color=blue]
      >
      > Well, since these are just exceptions, a simple try... except block
      > would be fine, and you can even figure out the reason for the
      > exception. Here is what I'd do:
      > - when you create your Tkinter main window, initialize an attribute
      > that you'll use to see if the application has quit, e.g
      > mainwin.hasQuit = False
      > - rewrite doquit this way:
      > def doquit():
      > if os.access('reca lc.tmp', os.F_OK):
      > os.remove('reca lc.tmp')
      > mainwin.hasQuit = True
      > pygame.quit()
      > - rewrite your custom event loop this way:
      > while 1:
      > gameLoop()
      > pygame.time.wai t(10)
      > try:
      > mainwin.update( )
      > except TclError:
      > if not mainwin.hasQuit :
      > raise
      >
      > And: (1) your problem should go away; (2) the "real" exceptions you
      > may get from Tkinter windows should not passed unnoticed.
      >
      >[/color]
      I adapted your suggestion slightly, and my final event loop looks like this:

      while not mainwin.hasQuit :
      try:
      gameLoop()
      except pygame.error:
      if not mainwin.hasQuit :
      raise
      pygame.time.wai t(10)
      try:
      mainwin.update( )
      except Tkinter.TclErro r:
      if not mainwin.hasQuit :
      raise

      That seems to work perfectly. Thanks, Eric!
      [color=blue]
      > Incomplete, but very useful:
      > http://www.pythonware.com/library/tk...tion/index.htm
      > But the best reference documentation you'll ever find is the tcl/tk
      > man pages, on-line here: http://www.tcl.tk/man/tcl8.4/TkCmd/contents.htm
      > It unfortunately requires to know how to convert the tcl/tk syntax to
      > Python/Tkinter syntax, but it is actually quite easy (mainly read
      > "option=val ue" when the tcl/tk documentation says "-option value")[/color]

      Ah, yes, I had managed to find the pythonware.com pages in my travels,
      and they proved quite helpful. Good to know about the man pages, too.
      (When I'm starting on a language feature, though, I usually find I learn
      a lot more from worked examples than from straight command information.
      I have friends who are just the opposite, so I suppose it's just a
      learning styles thing.)

      wxPython seems to be highly regarded. I might experiment with that for
      my next project, and see which framework I like the best. Thanks again.

      Comment

      • Eric Brunel

        #4
        Re: Mixing Txinter and Pygame

        On Wed, 23 Feb 2005 10:23:09 +1300, Tim Knauf <bulk@upperorbi t.com> wrote:
        [snip][color=blue]
        > (When I'm starting on a language feature, though, I usually find I learn
        > a lot more from worked examples than from straight command information.[/color]

        You may be interested in Tkinter best kept secret: the example scripts in the Demo/tkinter sub-directory of the Python source installation. It mainly covers the basics, but may be quite helpful when you start.

        HTH
        - eric -

        Comment

        Working...