I'm building a GUI which consists of a large array of toggle buttons (25 by 25) using wxPython. I have used GridSizer and AddMany to create the grid of togglebuttons. I'm not able to assign individual buttons to a variable with the AddMany syntax and hence, i'm not sure how I can track the clicked togglebuttons
This is my first attempt at using wxpython library to build an application and I'm not very familiar with its more powerful features. My past projects have only involved a few buttons and did not need a large grid of buttons
gs = wx.GridSizer(12, 12, 4, 4)
gs.AddMany( [(wx.ToggleButton(self, label='Cls'), 2, wx.EXPAND),
(wx.ToggleButton(self, label='Bck'), 2, wx.EXPAND),
(wx.ToggleButton(self, label='Close'), 2, wx.EXPAND),
(wx.ToggleButton(self, label='7'), 2, wx.EXPAND),
#lots of buttons
(wx.ToggleButton(self, label='8'), 0, wx.EXPAND),
(wx.ToggleButton(self, label='9'), 0, wx.EXPAND)])
I'm not able to find good advice or example on the internet and I'm hoping some experts of wxpython can help me here
You can make use of the label
or give each button a name
, whilst storing their current values in a dictionary
.
For example:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
self.button_dict = {'Info':False,'Error':False,'Question':False}
hbox = wx.BoxSizer()
sizer = wx.GridSizer(2, 2, 2, 2)
sizer.AddMany([wx.ToggleButton(panel, label='Info'), wx.ToggleButton(panel, label='Error'), wx.ToggleButton(panel, label='Question')])
hbox.Add(sizer, 0, wx.ALL, 15)
panel.SetSizer(hbox)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ShowButton)
self.SetSize((300, 200))
self.SetTitle('Buttons')
self.Centre()
self.Show(True)
def ShowButton(self, event):
x = event.GetEventObject()
lab = x.GetLabel()
print("Button pressed",lab)
if self.button_dict[lab]:
self.button_dict[lab] = False
else:
self.button_dict[lab] = True
print(self.button_dict)
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()