I'm working on a GUI and I'd like to create a variable number of buttons, depending on what is input by user. How should I go about this?
More precisely, the user opens a data file with a number of datasets, N. I would like to create N buttons with the ith button having the name of the ith dataset.
Something like... (conceptually, obviously this won't work)
import wx
# create sizer
hbox1=wx.GridBagSizer(4,4)
# create file button and add to sizer
fbtn = wx.Button(dv, label=file)
hbox1.Add(fbtn, pos=(jf,0))
# read file to get dataset names
dsetnames=[<dataset names for file>]
# create bottons for datasets found in file
jd=0 # datasets
for ds in dsetnames:
self.dbtn<jd> = wx.Button(dv, label=ds)
hbox1.Add(self.dbtn<jd>, pos=(jd,1))
jd+=1
Thank you.
I found a solution but I don't have the privilege yet to answer questions, so I'll add it here. Generally, the solution here is to create a dictionary with the object names as the keys and the objects as the values.
import wx
dv=wx.Panel(self)
# create sizer
hbox1=wx.GridBagSizer(4,4)
# create file button and add to sizer
# filename file input by user
fbtn = wx.Button(dv, label=file)
hbox1.Add(fbtn, pos=(jf,0))
# read file to get dataset names
dsetnames=[<dataset names for file>]
# create empty dictionary for objects
self.dbtn=dict()
# create bottons for datasets found in file
jd=0 # datasets
for ds in dsetnames:
objname=ds
self.dbtn.update({objname:wx.Button(dv, label=ds)}) # update dictionary with individual objects
hbox1.Add(self.dbtn[objname], pos=(jd,1))
jd+=1
I believe the following may be related.