hi,
I want to load an unknown number of buttons (20 max) into a panel.
When clicking a button, some other code should be executed.
Below code shows my attempt at solving the problem. Unfortunately execution results in an assertion error. Any help in pointing me in the right direction is much appreciated:
import wx
"""trying to bind an unknown number of images to click event"""
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1)
frame.Show()
self.SetTopWindow(frame)
return True
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Bind unknown number of images', size=(1000, 500))
panel = wx.Panel(self)
mainSizer = wx.GridBagSizer(hgap=5, vgap=5)
testSizer = wx.GridSizer(cols = 4, rows =0 )
images = "g1 g2 g3 g4 g5 ".split()
n=0
for image in images:
n=n+1
path = 'O:\Python Programming Bits\bind test\%s.gif' % image
image= wx.Image(path, wx.BITMAP_TYPE_ANY).Scale(80, 80).ConvertToBitmap()
button = wx.BitmapButton(panel, -1, bitmap=image, name=image)
button.Bind(wx.BitmapButton, self.OnButton)
testSizer.Add(button, flag=wx.EXPAND)
mainSizer.Add(testSizer, pos=(2, 2), span=(2, 5 ), flag=wx.EXPAND)
panel.SetSizer(mainSizer)
mainSizer.Fit(self)
mainSizer.SetSizeHints(self)
def OnButton(self, event):
print "you clicked on ... " + button_by_id.GetName()
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()
app.MainLoop()