Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can anyone tell me if pygame and Tkinter can work together?

167 views
Skip to first unread message

Nathan Pinno

unread,
Nov 15, 2005, 7:16:14 PM11/15/05
to
Hi,

Can anyone tell me if pygame and Tkinter can work together in the same
application? I thought I'd better ask before trying it, since both use
widgets.

Thanks,
Nathan Pinno

--
For great sites go to: http://www.the-web-surfers-store.com
MSN Messenger: falcon3166@hotmail,com
Yahoo! Messenger: spam_swatter31
ICQ: 199020705
AIM: f3mighty

--

.............................................................
> Posted thru AtlantisNews - Explore EVERY Newsgroup <
> http://www.AtlantisNews.com -- Lightning Fast!!! <
> Access the Most Content * No Limits * Best Service <

jep...@unpythonic.net

unread,
Nov 15, 2005, 8:28:28 PM11/15/05
to Nathan Pinno, pytho...@python.org
It's likely to be a challenge; each one has its own "event loop", and
the exact pitfalls of trying to use both at the same time probably
depend on the operating system too. For instance, on X, Tk and SDL
probably both expect full control of the handlers registered with
XSetErrorHandler and XSetIOErrorHandler, but Xlib only permits a single
handler.

Jeff

Nathan Pinno

unread,
Nov 15, 2005, 8:33:40 PM11/15/05
to jep...@unpythonic.net, pytho...@python.org
Thanks. I was going to use TKInter to pop up a message box, then use pygame
to run the game and display the score on the playing surface. Is this still
possible (I'm using Python 2.4.1 and Pygame 1.7.0 on WinXP with Service Pack
2)?

Nathan Pinno


Nathan Pinno,
Owner/operator of The Web Surfer's Store.
http://www.the-web-surfers-store.com/
MSN Messenger: falco...@hotmail.com
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705

jep...@unpythonic.net

unread,
Nov 15, 2005, 10:46:40 PM11/15/05
to Nathan Pinno, pytho...@python.org
On Tue, Nov 15, 2005 at 06:33:40PM -0700, Nathan Pinno wrote:
> Thanks. I was going to use TKInter to pop up a message box, then use pygame
> to run the game and display the score on the playing surface. Is this still
> possible (I'm using Python 2.4.1 and Pygame 1.7.0 on WinXP with Service Pack
> 2)?

This is more likely to work than the scenario I first considered, where both
GUI libraries would be in use at the same time.

With my understanding of how X works, and my knowledge of Tk, you'd probably be
successful if what you want to do is first use GUI Library A, and then GUI
Library B. This resolves the issue with XSetErrorHandler, for instance,
because you'll be completely done with Library A at the time you call the
initialization routines of Library B. It also resolves the event loop problem,
because you never need to allow Libraries A and B to receive events during the
same phase of the program.

I don't know anything about Windows, and I haven't actually given this scenario
a try on Linux either, so it's still all idle speculation on my part. Actual
experimentation on your part wouldn't be that hard, though. Just write the
simplest possible Tk program as a callable function 't()' and the simplest possible
pygame program as a callable function 'p()', and then see what happens when you run
them back to back:
def t():
import Tkinter
t = Tkinter.Tk()
Tkinter.Button(t, command = t.destroy).pack()
t.mainloop()
def p():
likewise
t()
p()

Jeff

Nathan Pinno

unread,
Nov 15, 2005, 11:45:01 PM11/15/05
to jep...@unpythonic.net, pytho...@python.org
Sounds good, I'll give it a try and see what happens, and report back about
my results.


Nathan Pinno,
Owner/operator of The Web Surfer's Store.
http://www.the-web-surfers-store.com/
MSN Messenger: falco...@hotmail.com
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705

-----Original Message-----
From: jep...@unpythonic.net [mailto:jep...@unpythonic.net]
Sent: November 15, 2005 8:47 PM
To: Nathan Pinno
Cc: pytho...@python.org
Subject: Re: Can anyone tell me if pygame and Tkinter can work together?

Nathan Pinno

unread,
Nov 16, 2005, 12:19:50 AM11/16/05
to
It worked, but unfornately I can't use this line as it brings up errors:

from Tkinter (or pygame) import *

Anyway around this little bug?

--
For great sites go to: http://www.the-web-surfers-store.com
MSN Messenger: falcon3166@hotmail,com
Yahoo! Messenger: spam_swatter31
ICQ: 199020705
AIM: f3mighty

"Nathan Pinno" <falco...@hotmail.com> wrote in message
news:mailman.724.11321163...@python.org...

--

Simon Brunning

unread,
Nov 16, 2005, 6:28:24 AM11/16/05
to pytho...@python.org
On 16/11/05, Nathan Pinno <falco...@hotmail.com> wrote:
> It worked, but unfornately I can't use this line as it brings up errors:
>
> from Tkinter (or pygame) import *
>
> Anyway around this little bug?

What's the error?

--
Cheers,
Simon B,
si...@brunningonline.net,
http://www.brunningonline.net/simon/blog/

Nathan Pinno

unread,
Nov 16, 2005, 2:38:05 PM11/16/05
to
It's a warning that says:

Can only use * in top level or something like that.

It's kind of annoying, but the program still ran after I made the import *
lines top level, and removed the def's.

Nathan Pinno.

--
For great sites go to: http://www.the-web-surfers-store.com
MSN Messenger: falcon3166@hotmail,com
Yahoo! Messenger: spam_swatter31
ICQ: 199020705
AIM: f3mighty

"Simon Brunning" <simon.b...@gmail.com> wrote in message
news:mailman.736.11321405...@python.org...

What's the error?

--

Fredrik Lundh

unread,
Nov 16, 2005, 3:03:06 PM11/16/05
to pytho...@python.org
Nathan Pinno wrote:

> It's a warning that says:
>
> Can only use * in top level or something like that.
>
> It's kind of annoying

why? importing tons of unknown stuff into a local namespace is
rather silly, and makes it impossible for the compiler to properly
analyze your code -- which means that you cannot use nested
scoping. the language reference says:

The from form with "*" may only occur in a module scope.
If the wild card form of import -- "import *" -- is used in a
function and the function contains or is a nested block with
free variables, the compiler will raise a SyntaxError.

future versions of Python will most likely issue a SyntaxError also
for "import *" in non-nested functions.

> but the program still ran after I made the import * lines top level,
> and removed the def's.

moving the "import *" line to the module scope would have been
enough; functions can refer to module-level variables just fine.

you might wish to read up on Python scoping rules:

http://docs.python.org/ref/naming.html

</F>

Nathan Pinno

unread,
Nov 18, 2005, 7:18:24 PM11/18/05
to
Even its from one or two external libraries like Tkinter and pygame? I
prefer to load what I need, not to constantly have to call it by its library
dot its_name. It makes easier coding for me. I try to keep my code as simply
as possible. Wonder if its possible to use a Tkinter question to make a
pygame run again (example being you get killed on a level and you want to
play again without exiting)?

--
For great sites go to: http://www.the-web-surfers-store.com
MSN Messenger: falcon3166@hotmail,com
Yahoo! Messenger: spam_swatter31
ICQ: 199020705
AIM: f3mighty

"Fredrik Lundh" <fre...@pythonware.com> wrote in message
news:mailman.757.11321718...@python.org...

--

0 new messages