Mixing Txinter and Pygame
Tim Knauf
bulk at upperorbit.com
Tue Feb 22 16:23:09 EST 2005
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.
More information about the Python-list
mailing list