473,466 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
3 3693
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 -
Jul 18 '05 #2
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.
Jul 18 '05 #3
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 -
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
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", pygame.mixer.get_init() print "Mixer channels",...
2
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. #----------------------------------------------- import sys...
1
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 respond that a button or hat arrow has been pressed. ...
0
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 generated with glade, with the exception of the socket...
1
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 screen -- how do you write this surface as an image...
11
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 do what it is supposed to do either.the code is a...
3
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 stays up and doesnt respond. i tried adding...
11
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 complained abiout words= so i guess its only the nbrs...
5
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 event.type == pygame.QUIT: sys.exit() ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.