This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 27th, 2012  |  syntax: Python  |  size: 3.01 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. working code for a stackoverflow.com question
  2. http://stackoverflow.com/questions/12135211/problems-displaying-wxbitmaps-using-wxpython
  3.  
  4. class viewWindow(wx.Frame):
  5.         imgSizer = (480,360)
  6.         def __init__(self, parent, title="View Window"):
  7.                 super(viewWindow,self).__init__(parent)
  8.                 ## create the menu and its sub trees
  9.                 menubar = wx.MenuBar()
  10.                 filemenu = wx.Menu()
  11.                 menubar.Append(filemenu, 'File')
  12.                 self.fitem = filemenu.Append(wx.ID_ANY, 'Open Connection Window')
  13.                 self.Bind(wx.EVT_MENU, self.openConnectionWindow, self.fitem)
  14.                 self.SetMenuBar(menubar)
  15.  
  16.                 ## here is where the actual stuff inside the frame is set up.
  17.                 self.pnl = wx.Panel(self)
  18.                 self.vbox = wx.BoxSizer(wx.VERTICAL)
  19.  
  20.                 ## create the wxImage for the web cam pic
  21.                 self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])
  22.                
  23.                 ## create the wxBitmap so that the wxImage can be displayed
  24.                 self.imageBit = wx.BitmapFromImage(self.image)
  25.                 self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)
  26.                
  27.                 ## add the staticBit to the sizer so it is rendered properly on resizes and such
  28.                 ## note: not actually needed to get the image to display, but reccommended for ease
  29.                 ## of layout
  30.                 self.vbox.Add(self.staticBit)
  31.  
  32.                 ## register the sizer with the panel so the panel knows to use it.
  33.                 self.pnl.SetSizer(self.vbox)
  34.                
  35.                 ## create a timer that will update the window based on frame rate
  36.                 self.timex = wx.Timer(self, wx.ID_OK)
  37.                 self.timex.Start(1000/framerate)
  38.                 self.Bind(wx.EVT_TIMER, self.redraw, self.timex)
  39.  
  40.                 ## set the size of the frame itself when it is first opened
  41.                 self.SetSize(self.imgSizer)
  42.                 self.Show()
  43.  
  44.         def openConnectionWindow(self, e):
  45.                 ## this will open a new connection window
  46.                 connect = connectionWindow(None)
  47.  
  48.         def redraw(self,e):
  49.                 ## this function updates the frame with the latest web cam image that has been
  50.                 ## retrieved by the client thread from the server.
  51.                
  52.                 ## get the newest image in the queue
  53.                 if not imgQ.empty():                        
  54.                         picz = imgQ.get()
  55.                         ## convert the image from a string to something usable (wxImage)
  56.                         self.image.SetData(picz)
  57.                         ## from wxImage to wxBitmap
  58.                         self.imageBit = wx.BitmapFromImage(self.image)
  59.                         self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)
  60.                         ## refresh the frame
  61.                         self.Refresh()
clone this paste RAW Paste Data