Feb-18-2018, 11:09 PM
I have been using tkinter for some time but I have seen so much about how much better wxpython is that I am trying to start using it. I am trying to create a program which reads from a list and creates buttons which are in a grid and which I can bind to an event handler. I have been searching for a few hours now but cannot figure out how to do it so I have started with this I found:
import wx class Example(wx.Frame): def __init__(self, parent, title): super(Example, self).__init__(parent, title = title,size = (300,200)) self.InitUI() self.Centre() self.Show() def InitUI(self): p = wx.Panel(self) gs = wx.GridSizer(4, 4, 5, 5) for i in range(1,17): btn = "Btn"+str(i) gs.Add(wx.Button(p,label = btn),0,wx.EXPAND) p.SetSizer(gs) # Adding the line below caused an error btn.bind('<Button-1>', onButton) # I have added this def to the original so I can test it def onButton(self, event): """ This method is fired when its corresponding button is pressed """ print ("Button pressed!") app = wx.App() Example(None, title = 'Grid demo') app.MainLoop()It gives a good layout and I can figure out how to adjust it to manage my list of 66 items. I cannot seem to work out how to get bind into the for loop.
Error:Traceback (most recent call last):
File "./first2wx.py", line 31, in <module>
Example(None, title = 'Grid demo')
File "./first2wx.py", line 8, in __init__
self.InitUI()
File "./first2wx.py", line 23, in InitUI
btn.bind('<Button-1>', onButton)
AttributeError: 'str' object has no attribute 'bind'