Skip to content

Instantly share code, notes, and snippets.

@fogleman
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogleman/9377043 to your computer and use it in GitHub Desktop.
Save fogleman/9377043 to your computer and use it in GitHub Desktop.
wxPython Bitmap Viewer
import wx
class View(wx.Panel):
def __init__(self, parent):
super(View, self).__init__(parent)
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.Bind(wx.EVT_SIZE, self.on_size)
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_CHAR_HOOK, self.on_char)
self.bitmap = None
def set_bitmap(self, bitmap):
self.bitmap = bitmap
self.Refresh()
def on_char(self, event):
if event.GetKeyCode() == wx.WXK_RETURN:
print 'Enter!'
def on_size(self, event):
event.Skip()
self.Refresh()
def on_paint(self, event):
dc = wx.AutoBufferedPaintDC(self)
dc.SetBackground(wx.WHITE_BRUSH)
dc.Clear()
if self.bitmap is None:
return
cw, ch = self.GetClientSize()
bw, bh = self.bitmap.GetSize()
x = cw / 2 - bw / 2
y = ch / 2 - bh / 2
dc.DrawBitmap(self.bitmap, x, y)
class Frame(wx.Frame):
def __init__(self):
super(Frame, self).__init__(None)
self.view = View(self)
self.SetTitle('Hello')
self.SetClientSize((800, 600))
self.Center()
def main():
app = wx.App(None)
frame = Frame()
frame.Show()
app.MainLoop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment