I like to create a list of dictionaries, where each dictionary contains the properties for one of my controls.
Buttons = [
{"Name": "Load", "Tip": "Click here to load (or re-load) the selected configuration."},
{"Name": "Save", "Tip": "Click here to save your current settings into the selected configuration."},
{"Name": "Go", "Tip": "Click here to proceed using the current settings."},
{"Name": "Cancel", "Tip": "Click here to exit without doing anything."},
]
I then loop through the list and create the controls and bind them all to a single method (OnClick or OnChange, or whatever).
for btn in Buttons:
b = wx.Button(pnl, -1, label=btn[“Name”], name=btn[“Name”])
b.SetToolTip(wx.ToolTip(btn["Tip"]))
boxTopRight.Add(b, 0, wx.ALIGN_CENTER|wx.ALL)
boxTopRight.Add((10,10), 0, wx.EXPAND)
self.Bind(wx.EVT_BUTTON, self.OnClick, b)
My OnClick looks like this:
def OnClick(self, event):
btn = event.GetEventObject().Name
if (btn == "Cancel"):
etc...
but this could probably be a dictionary instead of a bunch of ifs…
···
On Thu, Jul 10, 2008 at 10:03 AM, raffaello barbarossa.platz@gmail.com wrote:
Sometimes in a frame I need build a whole set of controls (usually buttons or wx.textCtrl). Since they differ only for a few properties, the pythonic way would be to out all those properties in a list and pass it to a “for” loop. But this does not work with the variables used to identify these controls at frame scope. For example:
btClose = btHide = btShow = None
and then in init:
…
params = [[‘Exit’, pos, self.btClose], etc.]
for i in range(len(params)):
bt = self.BuildButton(params[i])self.btClose.Bind(etc)
brings about the exception “AttributeError: None object has no attribute ‘Bind’”
It seems that if you pass an argument the function receives its value, instead of its address.
The only workaround solution I could devise was a set of
if i == 0: self.btClose = bt
elif i == 1: self.btHide = bt, etc.that, at least to me, seems more pachidermic than pythonic.
Anybody has a better solution?