[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] wxPython and pygame - pygame window will not disappear



> Cool!  Is there a way to do it without classes and
> in a pygame window?
> 


Ian, see from Rene's note: http://pygame.org/wiki/gui

Using wxPython with Pygame doesn't mean you can put
GUI items inside your pygame window, and right now
pygame pops up its own window anyway when called from
wxpython so its the same as if you did an exec("python
my_pygame.py") from your wxpython app. 

If you want to try it out, look at the last code
example here:

http://wiki.wxpython.org/IntegratingPyGame

And I've attached a template for where you put your
pygame code. So just use the wiki wxpython example,
and import wxpy_pygame then replace SDLThread with
GameThread. Very simple. But its broken as I can't get
the pygame window to close. 

You have to use classes because everyone who wrote the
example code you are using did :)

Mark





      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping
#
#  Example on how to create a PyGame that will run standalone
#  as well as part of a wxPython project.
#
#  See http://wiki.wxpython.org/IntegratingPyGame for the wxPython code
#
#  This window simply closes itself when the user presses escape.
#  Add your code inside the MainClass __init__ and tick() functions.
#

import pygame
from pygame.locals import *
import thread


class GameThread:
    """
    GameThread class expects to be created from a wxPython app where 
        surface : surface = pygame.display.set_mode(self.GetClientSizeTuple())
        func : is a function in the wxPython app to be called when the python app ends.
    """

    def __init__(self,surface, func):
        self.m_bKeepGoing = self.m_bRunning = False
        self.surface = surface
        self.threadDoneHandler = func
  
    def Start(self):
	
	self.mainclass = MainClass(self.surface)
     
        self.m_bKeepGoing = self.m_bRunning = True
        thread.start_new_thread(self.Run, ())
 
    def Stop(self):
        self.m_bKeepGoing = False
   
    def IsRunning(self):
        return self.m_bRunning
   
    def Run(self):
        while self.m_bKeepGoing:

            self.mainclass.tick()

            if self.mainclass.done:
                self.m_bKeepGoing = False

        self.m_bRunning = False
        self.threadDoneHandler()


class MainClass:
    done = False
    def __init__(self, surface):
        
        # Initialization code goes here
        self.surface = surface
		
    def tick(self):

        # This is your pygame event loop, put your code here.
        
        for event in pygame.event.get():
            if event.type == QUIT or \
               (event.type == KEYDOWN and event.key == K_ESCAPE):
                self.done = True 
                
                            

def main():
    # We were called from a command line, initialise the screen and start the MainClass up.
    pygame.init()
    screen = pygame.display.set_mode((640,480))
    pygame.display.set_caption('Pygame inside wxPython!')
	
    mainclass = MainClass(screen)

    # Event loop
    while mainclass.done == False:

        mainclass.tick()


if __name__ == '__main__': main()