|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +AutoSizeBitmap.py |
| 5 | +
|
| 6 | +Example for how to have a bitmap autosize itself in wxPython |
| 7 | +""" |
| 8 | + |
| 9 | +import wx |
| 10 | + |
| 11 | +class AutoSizeBitmap(wx.Window): |
| 12 | + """ |
| 13 | + A subclass of wx.Window that will hold an image (much like a StaticBitmap), |
| 14 | + but re-size it to fit the current size of the Window |
| 15 | +" """ |
| 16 | + def __init__(self, parent, image, *args, **kwargs): |
| 17 | + """ |
| 18 | + initialize an AutoSizeBitmap |
| 19 | + |
| 20 | + :param parent: parent Window for this window |
| 21 | + :param image: a wx.Image that you want to display |
| 22 | + """ |
| 23 | + |
| 24 | + wx.Window.__init__(self, parent, *args, **kwargs) |
| 25 | + |
| 26 | + self.orig_image = image |
| 27 | + self.bitmap = None |
| 28 | + |
| 29 | + self.Bind(wx.EVT_SIZE, self.OnSize) |
| 30 | + self.Bind(wx.EVT_PAINT, self.OnPaint) |
| 31 | + |
| 32 | + def OnSize(self, evt=None): |
| 33 | + img = self.orig_image.Copy() |
| 34 | + img.Rescale(*self.Size) |
| 35 | + self.bitmap = wx.BitmapFromImage(img) |
| 36 | + |
| 37 | + def OnPaint(self, evt=None): |
| 38 | + dc = wx.PaintDC(self) |
| 39 | + dc.DrawBitmap(self.bitmap,0,0) |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + import sys |
| 43 | + |
| 44 | + try: |
| 45 | + filename = sys.argv[1] |
| 46 | + except: |
| 47 | + filename = "Images/cute_close_up.jpg" |
| 48 | + App = wx.App(False) |
| 49 | + f = wx.Frame(None) |
| 50 | + img = wx.Image(filename) |
| 51 | + b = AutoSizeBitmap(f, img) |
| 52 | + f.Show() |
| 53 | + App.MainLoop() |
| 54 | + |
0 commit comments