Paste Code
Paste Blends
Paste Images
### PYGAME IN WX ###
# A simple test of embedding Pygame in a wxPython frame
#
# By David Barker (aka Animatinator), 14/07/2010


import wx, sys, os, pygame

class PygameDisplay(wx.Window):
def __init__(self, parent, ID):
wx.Window.__init__(self, parent, ID)
self.parent = parent
self.hwnd = self.GetHandle()
os.environ['SDL_VIDEODRIVER'] = 'windib'
os.environ['SDL_WINDOWID'] = str(self.hwnd)

pygame.display.init()
self.screen = pygame.display.set_mode()
self.size = self.GetSizeTuple()

self.timer = wx.Timer(self)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.Update, self.timer)
self.Bind(wx.EVT_SIZE, self.OnSize)

self.fps = 60.0
self.timespacing = 1000.0 / self.fps
self.timer.Start(self.timespacing, False)

self.linespacing = 5

def Update(self, event):
# Any update tasks would go here (moving sprites, advancing animation frames etc.)
self.Redraw()

def Redraw(self):
self.screen.fill((0, 0, 0))

cur = 0

while cur <= self.size[1]:
pygame.draw.aaline(self.screen, (255, 255, 255), (0, self.size[1] - cur), (cur, 0))

cur += self.linespacing

pygame.display.update()

def OnPaint(self, event):
self.Redraw()

def OnSize(self, event):
self.size = self.GetSizeTuple()

def Kill(self, event):
# Make sure Pygame can't be asked to redraw /before/ quitting by unbinding all methods which
# call the Redraw() method
# (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame)
self.Unbind(event = wx.EVT_PAINT, handler = self.OnPaint)
self.Unbind(event = wx.EVT_TIMER, handler = self.Update, source = self.timer)

class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1)

self.display = PygameDisplay(self, -1)

self.statusbar = self.CreateStatusBar()
self.statusbar.SetFieldsCount(3)
self.statusbar.SetStatusWidths([-3, -4, -2])
self.statusbar.SetStatusText("wxPython", 0)
self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1)

self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_CLOSE, self.Kill)

self.curframe = 0

self.SetTitle("Pygame embedded in wxPython")

self.slider = wx.Slider(self, wx.ID_ANY, 5, 1, 10, style = wx.SL_HORIZONTAL | wx.SL_LABELS)
self.slider.SetTickFreq(0.1, 1)

self.timer = wx.Timer(self)

self.Bind(wx.EVT_SCROLL, self.OnScroll)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_TIMER, self.Update, self.timer)

self.timer.Start((1000.0 / self.display.fps))

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.slider, 0, flag = wx.EXPAND)
sizer.Add(self.display, 1, flag = wx.EXPAND)

self.SetAutoLayout(True)
self.SetSizer(sizer)
self.Layout()

def Kill(self, event):
self.display.Kill(event)
pygame.quit()
self.Destroy()

def OnSize(self, event):
self.Layout()

def Update(self, event):
self.curframe += 1
self.statusbar.SetStatusText("Frame %i" % self.curframe, 2)

def OnScroll(self, event):
self.display.linespacing = self.slider.GetValue()

class App(wx.App):
def OnInit(self):
self.frame = Frame(parent = None)
self.frame.Show()
self.SetTopWindow(self.frame)

return True

if __name__ == "__main__":
app = App()
app.MainLoop()
  1. ### PYGAME IN WX ###
  2. # A simple test of embedding Pygame in a wxPython frame
  3. #
  4. # By David Barker (aka Animatinator), 14/07/2010
  5.  
  6.  
  7. import wx, sys, os, pygame
  8.  
  9. class PygameDisplay(wx.Window):
  10.     def __init__(self, parent, ID):
  11.         wx.Window.__init__(self, parent, ID)
  12.         self.parent = parent
  13.         self.hwnd = self.GetHandle()
  14.         os.environ['SDL_VIDEODRIVER'] = 'windib'
  15.         os.environ['SDL_WINDOWID'] = str(self.hwnd)
  16.        
  17.         pygame.display.init()
  18.         self.screen = pygame.display.set_mode()
  19.         self.size = self.GetSizeTuple()
  20.        
  21.         self.timer = wx.Timer(self)
  22.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  23.         self.Bind(wx.EVT_TIMER, self.Update, self.timer)
  24.         self.Bind(wx.EVT_SIZE, self.OnSize)
  25.        
  26.         self.fps = 60.0
  27.         self.timespacing = 1000.0 / self.fps
  28.         self.timer.Start(self.timespacing, False)
  29.  
  30.         self.linespacing = 5
  31.  
  32.     def Update(self, event):
  33.         # Any update tasks would go here (moving sprites, advancing animation frames etc.)
  34.         self.Redraw()
  35.  
  36.     def Redraw(self):
  37.         self.screen.fill((0, 0, 0))
  38.  
  39.         cur = 0
  40.  
  41.         while cur <= self.size[1]:
  42.             pygame.draw.aaline(self.screen, (255, 255, 255), (0, self.size[1] - cur), (cur, 0))
  43.            
  44.             cur += self.linespacing
  45.        
  46.         pygame.display.update()
  47.  
  48.     def OnPaint(self, event):
  49.         self.Redraw()
  50.  
  51.     def OnSize(self, event):
  52.         self.size = self.GetSizeTuple()
  53.  
  54.     def Kill(self, event):
  55.         # Make sure Pygame can't be asked to redraw /before/ quitting by unbinding all methods which
  56.         # call the Redraw() method
  57.         # (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame)
  58.         self.Unbind(event = wx.EVT_PAINT, handler = self.OnPaint)
  59.         self.Unbind(event = wx.EVT_TIMER, handler = self.Update, source = self.timer)
  60.  
  61. class Frame(wx.Frame):
  62.     def __init__(self, parent):
  63.         wx.Frame.__init__(self, parent, -1)
  64.        
  65.         self.display = PygameDisplay(self, -1)
  66.        
  67.         self.statusbar = self.CreateStatusBar()
  68.         self.statusbar.SetFieldsCount(3)
  69.         self.statusbar.SetStatusWidths([-3, -4, -2])
  70.         self.statusbar.SetStatusText("wxPython", 0)
  71.         self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1)
  72.        
  73.         self.Bind(wx.EVT_SIZE, self.OnSize)
  74.         self.Bind(wx.EVT_CLOSE, self.Kill)
  75.        
  76.         self.curframe = 0
  77.        
  78.         self.SetTitle("Pygame embedded in wxPython")
  79.        
  80.         self.slider = wx.Slider(self, wx.ID_ANY, 5, 1, 10, style = wx.SL_HORIZONTAL | wx.SL_LABELS)
  81.         self.slider.SetTickFreq(0.1, 1)
  82.        
  83.         self.timer = wx.Timer(self)
  84.        
  85.         self.Bind(wx.EVT_SCROLL, self.OnScroll)
  86.         self.Bind(wx.EVT_SIZE, self.OnSize)
  87.         self.Bind(wx.EVT_TIMER, self.Update, self.timer)
  88.        
  89.         self.timer.Start((1000.0 / self.display.fps))
  90.        
  91.         sizer = wx.BoxSizer(wx.VERTICAL)
  92.         sizer.Add(self.slider, 0, flag = wx.EXPAND)
  93.         sizer.Add(self.display, 1, flag = wx.EXPAND)
  94.        
  95.         self.SetAutoLayout(True)
  96.         self.SetSizer(sizer)
  97.         self.Layout()
  98.  
  99.     def Kill(self, event):
  100.         self.display.Kill(event)
  101.         pygame.quit()
  102.         self.Destroy()
  103.  
  104.     def OnSize(self, event):
  105.         self.Layout()
  106.  
  107.     def Update(self, event):        
  108.         self.curframe += 1
  109.         self.statusbar.SetStatusText("Frame %i" % self.curframe, 2)
  110.  
  111.     def OnScroll(self, event):
  112.         self.display.linespacing = self.slider.GetValue()
  113.  
  114. class App(wx.App):
  115.     def OnInit(self):
  116.         self.frame = Frame(parent = None)
  117.         self.frame.Show()
  118.         self.SetTopWindow(self.frame)
  119.        
  120.         return True
  121.  
  122. if __name__ == "__main__":
  123.     app = App()
  124.     app.MainLoop()
  125.  
go to heaven