Hi all
I have managed to integrate a PyGame panel into a wxPython frame as described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame
But now, I want to add wx widgets to the frame. Unofrtunately, this does not work as expected. It seems to be impossible to add the PygGame panel into sizers and create a nice interface around it. Imagine a few buttons above or below the panel. How could this be done?
My first try was to add a wx.Panel behind the PyGame panel and this worked so far as I could have wx wodgets below, but the background was transparent!!! It is also possible to add a toolbar, but it is very small. And why does the frame not resize to include the full size of the PyGame panel (frame size is equal to panel size, so the panel is truncated).
Another problem is that there can only be one PyGame panel. This is a PyGame problem, but if somebody happens to know how to run multiple PyGame screens I am very interested, of course.
Any help is very much appreciated!
thanks
André
PS Here is the working code so far:
import wx
import os
import thread
pyGame = None
class SDLThread(object):
def init(self, screen):
self.m_bKeepGoing = self.m_bRunning = False
self.screen = screen
self.surface = pyGame.display.get_surface()
self.rect = self.surface.get_rect()
def Start(self):
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):
self.setup()
while self.m_bKeepGoing:
if pyGame:
self.update()
self.m_bRunning = False;
def setup(self):
self.color = (255, 0, 0)
self.r = (10, 10, 100, 100)
def update(self):
e = pyGame.event.poll()
if e.type == pyGame.MOUSEBUTTONDOWN:
self.color = (255, 0, 128)
self.r = (e.pos[0], e.pos[1], 100, 100)
print e.pos
self.screen.fill((0, 0, 0))
self.screen.fill(self.color, self.r)
pyGame.draw.aaline(self.screen, (63, 191, 128), (0, 0), (
self.rect.width-1, self.rect.height-1))
pyGame.draw.aaline(self.screen, (63, 191, 128), (self.rect.width-1, 0), (0, self.rect.height-1))
pyGame.display.flip()
class SDLPanel(wx.Panel):
def init(self, parent, ID, game_size):
global pyGame
wx.Panel.__init__(self, parent, ID, size=game_size)
self.Fit()
os.environ['SDL_WINDOWID'] = str(self.GetHandle())
os.environ['SDL_VIDEODRIVER'] = 'windib'
import pygame
pyGame = pygame
pyGame.display.init()
window = pyGame.display.set_mode(game_size)
self.thread = SDLThread(window)
self.thread.Start()
class MyFrame(wx.Frame):
def init(self, parent, ID, title, game_size):
width, height = game_size
wx.Frame.init(self, parent, ID, title, size=(width+39, height+34))
self.pnlSDL
= SDLPanel(self, -1, game_size)
outerbox = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(outerbox)
···
width, height = game_size
gamepanel = wx.Panel(self, 998, pos=wx.Point(0, 0), size=
wx.Size(width, height))
outerbox.Add(gamepanel)
wxbox = wx.BoxSizer(wx.HORIZONTAL)
outerbox.Add(wxbox)
p = wx.Panel(self, 1000, pos=wx.Point(0, 0))
outerbox.Add
(wx.Panel(self, 1000, pos=wx.Point(0, 0)))
wxbox.Add(wx.Button(self, 1001, “Start”), flag=wx.ALL, border=3)
wxbox.Add(wx.Button(self, 1002, “Stop”), flag=wx.ALL, border=3)
self.SetAutoLayout(True)
self.Fit()
toolbar = self.CreateToolBar(style=wx.TB_VERTICAL | wx.TB_TEXT | wx.NO_BORDER | wx.TB_FLAT)
id_start = 2001
toolbar.AddLabelTool
(id_start, label=‘Start’, bitmap=wx.Bitmap(‘start.png’), shortHelp=‘Start Simulation’)
wx.EVT_TOOL(self, id_start, self.OnStartSimulation)
id_stop = 2002
toolbar.AddLabelTool(id_stop, label='Stop', bitmap=
wx.Bitmap(‘stop.png’), shortHelp=‘Stop Simulation’)
wx.EVT_TOOL(self, id_stop, self.OnStopSimulation)
id_quit = 2003
toolbar.AddLabelTool(id_quit, label='Quit', bitmap=wx.Bitmap
(‘quit.png’), shortHelp=‘Quit Simulation’)
wx.EVT_TOOL(self, id_quit, self.OnCloseWindow)
toolbar.Realize()
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
def OnStartSimulation(self, event):
"""Start button pressed"""
print 'Start', event
def OnStopSimulation(self, event):
"""Stop button pressed"""
print 'Stop', event
def OnSize(self, event):
size = self.GetClientSize()
print 'resizing', size
if getattr(self, 'app', None):
self.app.update
()
event.Skip()
def OnCloseWindow(self, event):
self.Destroy()
app = wx.PySimpleApp()
frame = MyFrame(None, -1, “Danger Island”, (800,600))
frame.Show()
app.MainLoop
()