Skip to content

Commit 16ded0f

Browse files
added aspect ratio preservation to AutoSizeBitmap demo
1 parent 9110851 commit 16ded0f

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

AutoSizeBitmap.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,83 @@
55
66
Example for how to have a bitmap autosize itself in wxPython
77
"""
8-
8+
import math
99
import wx
10-
print "using wxPython version:", wx.__version__
10+
11+
KEEP_ASPECT_RATIO = True
12+
13+
14+
class AutoSizeFrame(wx.Frame):
15+
def __init__(self, image, *args, **kwargs):
16+
wx.Frame.__init__(self, *args, **kwargs)
17+
18+
self.aspect_ratio = float(image.Width) / float(image.Height)
19+
self.canvas = AutoSizeBitmap(image, self)
20+
self.canvas.SetSize((300, 400))
21+
22+
self.Bind(wx.EVT_SIZE, self.OnSize)
23+
24+
self.Fit()
25+
self.Show()
26+
27+
def OnSize(self, evt=None):
28+
size = self.Size
29+
if (size[0] > 0 and size[1] > 0):
30+
width, height = size
31+
if KEEP_ASPECT_RATIO:
32+
total_size = width * height
33+
height = int(math.sqrt(total_size / self.aspect_ratio))
34+
width = int(total_size / height)
35+
# resize window on the fly to keep the aspect ratio
36+
self.SetSize((width, height))
37+
self.canvas.SetSize((width, height))
38+
1139

1240
class AutoSizeBitmap(wx.Window):
1341
"""
1442
A subclass of wx.Window that will hold an image (much like a StaticBitmap),
1543
but re-size it to fit the current size of the Window
16-
" """
17-
def __init__(self, parent, image, *args, **kwargs):
44+
"""
45+
def __init__(self, image, *args, **kwargs):
1846
"""
1947
initialize an AutoSizeBitmap
20-
48+
2149
:param parent: parent Window for this window
2250
:param image: a wx.Image that you want to display
23-
"""
24-
wx.Window.__init__(self, parent, *args, **kwargs)
51+
"""
52+
wx.Window.__init__(self, *args, **kwargs)
2553

2654
self.orig_image = image
2755
self.bitmap = None
28-
56+
self.prev_size = self.Size
57+
2958
self.Bind(wx.EVT_SIZE, self.OnSize)
3059
self.Bind(wx.EVT_PAINT, self.OnPaint)
3160

3261
def OnSize(self, evt=None):
33-
img = self.orig_image.Copy()
34-
img.Rescale(*self.Size)
35-
self.bitmap = wx.BitmapFromImage(img)
36-
self.Refresh()
62+
size = self.Size
63+
if size[0] > 0 and size[1] > 0:
64+
img = self.orig_image.Copy()
65+
img.Rescale(*size)
66+
self.bitmap = wx.BitmapFromImage(img)
67+
self.Refresh()
3768

3869
def OnPaint(self, evt=None):
3970
dc = wx.PaintDC(self)
4071
try:
41-
dc.DrawBitmap(self.bitmap,0,0)
42-
except ValueError: # in case bitmap has not yet been initialized
72+
dc.DrawBitmap(self.bitmap, 0, 0)
73+
except ValueError: # in case bitmap has not yet been initialized
4374
pass
4475

4576
if __name__ == "__main__":
4677
import sys
47-
48-
try:
78+
79+
try:
4980
filename = sys.argv[1]
5081
except:
5182
filename = "Images/cute_close_up.jpg"
5283
App = wx.App(False)
53-
f = wx.Frame(None)
5484
img = wx.Image(filename)
55-
b = AutoSizeBitmap(f, img)
56-
f.Show()
85+
f = AutoSizeFrame(img, None, size=(400, 600))
5786
App.MainLoop()
5887

ScrolledBitmap.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/env
22

3+
"""
4+
Putting an image in a Scrolled Window
5+
6+
And drawing something over it.
7+
"""
8+
39
import wx
410

511
# set an image file here

0 commit comments

Comments
 (0)