This is not a real program but a feasibility test.
The purpose is to:
1. create a bunch of bitmapbuttons on a scrolling window from the contents of an image directory.
2. When the user clicks on their favorite button something should happen.
The nice part is that 1 is working very well. The sticking point is two. I've tried various tips from the web without much success.
I'm having minor problems in line 7 and 24 but if I can't get line 30 working so I can actually use the newly created buttons those problems won't matter much. (the lines in question are commented in the code with my thoughts or guesses)
#!/usr/bin/python
import wx
import glob
filenames = glob.glob('/home/jim/Documents/python-programming/swick/thumbs/*.jpg')
#filenames = glob.glob("{*.jpg,*.png}") not possible ?
class TestFrame(wx.Frame):
def __init__(self, parent):
count = 0
wx.Frame.__init__(self, parent, title="Loading Images")
# hook some mouse events
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
self.p = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL)
self.p.SetScrollRate(10, 10)
fgs = wx.FlexGridSizer(cols=1, hgap=10, vgap=10)
self.p.SetSizer(fgs)
for name in filenames:
img1 = wx.Image(name, wx.BITMAP_TYPE_ANY)
img2 = img1.Scale(256, 192)
#how to us img2 on bitmapbutton ?
# or import image and use Image.open and .thumbnail
# right now I'm using a directory of image that have already been scaled to the size I want so the program works
# as far as creating new buttons with images from that directory and adding them to the sizer
snb = wx.BitmapButton(self.p, count, wx.Bitmap(name, wx.BITMAP_TYPE_ANY))
#next line is an attempt to actually use the button we just created - this is the main sticking point
# snb appears to be ws._controls.bitmapbutton proxy of <Swig object of type wx.bitmapbutton * # in other words a pointer
snb.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
# the bind works in so far as I can detect clicks on the button
count += 1
fgs.Add(snb)
def OnLeftDown(self, event):
"""left mouse button is pressed"""
pt = event.GetPosition() # position tuple
print pt
self.SetTitle('LeftMouse = ' + str(pt))
eo = GetId(self)
print eo
def OnRightDown(self, event):
"""right mouse button is pressed"""
pt = event.GetPosition()
self.SetTitle('RightMouse = ' + str(pt))
for child in self.GetChildren() :
print "ox"
child.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
child.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
child.Bind(wx.EVT_MOTION, self.OnMotion)
#self.p.SetSizerAndFit(self)
#self.Fit()
app = wx.PySimpleApp()
frm = TestFrame(None)
frm.Show()
app.MainLoop()
Thanks in advance for your help.