Skip to content

Commit 9110851

Browse files
updated ScrolledBitmap demo to add an overlayed rectangle
1 parent c8326dc commit 9110851

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

ScrolledBitmap.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,62 @@
1-
#!/usr/bin/env python2.4
1+
#!/usr/bin/env
22

3-
from wxPython.wx import *
3+
import wx
44

5+
# set an image file here
6+
img_filename = 'Images/white_tank.jpg'
57

6-
class MyCanvas(wxScrolledWindow):
7-
def __init__(self, parent, id = -1, size = wxDefaultSize):
8-
wxScrolledWindow.__init__(self, parent, id , wxPoint(0, 0), size, wxSUNKEN_BORDER)
9-
##wxScrolledWindow.__init__(self, parent)
10-
## read the image in (this is not a good place to do this in a real app)
118

12-
print "about to Init"
9+
class MyCanvas(wx.ScrolledWindow):
10+
def __init__(self, *args, **kwargs):
11+
wx.ScrolledWindow.__init__(self, *args, **kwargs)
1312

14-
wxInitAllImageHandlers()
13+
self.bmp = wx.Image(img_filename).ConvertToBitmap()
1514

16-
print "done initing"
15+
self.maxWidth, self.maxHeight = self.bmp.GetWidth(), self.bmp.GetHeight()
1716

18-
#img = wxImage("white_tank.jpg",wxBITMAP_TYPE_JPEG )
19-
#img = wxImage("white_tank.jpg")
20-
#bmp = img.ConvertToBitmap()
21-
#jpg = wxImage(opj('bitmaps/image.jpg'), wxBITMAP_TYPE_JPEG).ConvertToBitmap()
17+
self.SetScrollbars(20, 20, self.maxWidth / 20, self.maxHeight / 20)
2218

23-
self.bmp = wxImage('Images/white_tank.jpg', wxBITMAP_TYPE_JPEG ).ConvertToBitmap()
19+
self.Bind(wx.EVT_PAINT, self.OnPaint)
2420

25-
print "done loading image"
26-
27-
self.maxWidth, self.maxHeight = self.bmp.GetWidth(), self.bmp.GetHeight()
28-
29-
self.SetScrollbars(20, 20, self.maxWidth/20, self.maxHeight/20)
30-
31-
EVT_PAINT(self, self.OnPaint)
21+
# an arbitrary rect to draw -- in pixel coords
22+
self.rect = (200, 200, 300, 200) # x, y, width, height
3223

3324
def OnPaint(self, event):
34-
dc = wxPaintDC(self)
25+
dc = wx.PaintDC(self)
3526
self.PrepareDC(dc)
36-
dc.DrawBitmap(self.bmp, 0, 0)
3727

28+
dc.SetPen(wx.Pen(wx.RED, 2))
29+
dc.SetBrush(wx.Brush((255, 255, 255, 85)))
3830

39-
class TestFrame(wxFrame):
40-
def __init__(self,parent, id,title,position,size):
41-
wxFrame.__init__(self,parent, id,title,position, size)
31+
dc.DrawBitmap(self.bmp, 0, 0)
32+
dc.DrawRectangle(*self.rect)
4233

4334

44-
EVT_CLOSE(self, self.OnCloseWindow)
35+
class TestFrame(wx.Frame):
36+
def __init__(self, *args, **kwargs):
37+
wx.Frame.__init__(self, *args, **kwargs)
4538

39+
wx.EVT_CLOSE(self, self.OnCloseWindow)
4640

47-
self.Canvas1 = MyCanvas(self, wxNewId() )
41+
self.Canvas1 = MyCanvas(self, wx.NewId())
4842

4943
def OnCloseWindow(self, event):
5044
self.Destroy()
5145

52-
class App(wxApp):
46+
47+
class App(wx.App):
5348
def OnInit(self):
54-
frame = TestFrame(NULL, -1, "Scroll Test", wxDefaultPosition,(550,200))
49+
frame = TestFrame(None, title="Scroll Test", size=(800, 700))
5550
self.SetTopWindow(frame)
5651
frame.Show(True)
57-
return true
52+
return True
5853

5954
if __name__ == "__main__":
6055

61-
app = App(0)
62-
print "about to start Mainloop"
56+
app = App(False)
6357

6458
app.MainLoop()
65-
59+
6660

6761

6862

0 commit comments

Comments
 (0)