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 3 3604
On Tue, 22 Feb 2005 13:17:37 +1300, Tim Knauf <ti*@upperorbit.com> 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/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=value" when the tcl/tk documentation says "-option value")
HTH
- Eric Brunel -
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/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=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.
On Wed, 23 Feb 2005 10:23:09 +1300, Tim Knauf <bu**@upperorbit.com> 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 - This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marian Aldenhövel |
last post by:
Hi,
I am trying to make pygame play music on windows. This simple program:
import pygame,time
pygame.init()
print "Mixer settings",...
|
by: Brent W. Hughes |
last post by:
I'm just starting to learn pygame. I write what I think is just about the
simplest program that should display a window and then quit....
|
by: kjm |
last post by:
Hi everyone,
I have recently acquired a Logitech Rumble pad to use as an input
device. I have been having trouble getting the event que to...
|
by: Lunpa |
last post by:
My project:
I'm working on a game, where in the ui, it takes the pygame window, and
shoves it into a gtk2 socket widget. (gtk2 widgets are...
|
by: liuliuliu |
last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a
pygame display? i have a surface which is basically the entire visible...
|
by: dynamo |
last post by:
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not...
|
by: globalrev |
last post by:
im doing this :
http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/
and when closing the program the window...
|
by: globalrev |
last post by:
http://www.pygame.org/docs/ref/mixer.html
import pygame
#pygame.mixer.init(frequency=22050, size=-16, channels=2,
buffer=3072) //it...
|
by: defn noob |
last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i
am looping using:
while 1:
for event in pygame.event.get():
if...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |