Re: Mixing Txinter and Pygame
On Wed, 23 Feb 2005 10:23:09 +1300, Tim Knauf [EMAIL PROTECTED] wrote: [snip] (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. 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 - -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Txinter and Pygame
On Tue, 22 Feb 2005 13:17:37 +1300, Tim Knauf [EMAIL PROTECTED] wrote: 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. 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('recalc.tmp', os.F_OK): os.remove('recalc.tmp') mainwin.hasQuit = True pygame.quit() - rewrite your custom event loop this way: while 1: gameLoop() pygame.time.wait(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. 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? :^) Incomplete, but very useful: http://www.pythonware.com/library/tkinter/introduction/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=value when the tcl/tk documentation says -option value) HTH - Eric Brunel - -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Txinter and Pygame
Eric Brunel wrote: 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('recalc.tmp', os.F_OK): os.remove('recalc.tmp') mainwin.hasQuit = True pygame.quit() - rewrite your custom event loop this way: while 1: gameLoop() pygame.time.wait(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. 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.wait(10) try: mainwin.update() except Tkinter.TclError: if not mainwin.hasQuit: raise That seems to work perfectly. Thanks, Eric! Incomplete, but very useful: http://www.pythonware.com/library/tkinter/introduction/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=value when the tcl/tk documentation says -option value) 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. -- http://mail.python.org/mailman/listinfo/python-list
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.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 -- http://mail.python.org/mailman/listinfo/python-list