"Doug Holton" <d.holton@vanderbilt.edu> wrote in message
<snip>
And the new Bind method for events is an improvement:
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, theButton)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
but why not go all the way and allow for event binding that is even
simpler than in other gui toolkits:
self.Bind("size", self.OnSize) # window event
theButton.Bind(self.OnClick) # default button event
file_exit.Bind(self.OnFileExit) # default menu event
One thing to remember about that sort of syntax is that your events are
bound to methods of the button or other control, not to methods of the
frame.
Personally, I tend to wrap things like the creation and binding of controls
in factory functions, so that the exact syntax used doesn't matter much - it
is only used once in the function definition. As an example, I make buttons
using the function:
def MakeButton(win, text, sizer = None, func = None) :
btn = wx.Button(win, -1, text)
if func :
wx.EVT_BUTTON(win, btn.GetId(), func)
if sizer :
SizerAdd(sizer, btn)
return btn
(Where SizerAdd is yet another factory function.) This means the
form-building part of the code has lines like:
MakeButton(self, "Load...", hs, self.OnLoad)
It's only the code that is different for each button that is specified in
the main code. The GetId() function is very useful, and saves all sorts of
messing around with ids.
I dislike the idea of using strings to identify functionality in the way you
suggest. Although python will not spot mistakes like wx.EVT_SIZ at
"compile" time, tools like pychecker can, while they have no chance with
"siz" vs. "size". You can also get more information from a python shell -
if you can't remember whether it should be wx.EVT_SIZ or wx.EVT_SIZE, you
can open a python shell and type
import wx
[x for x in dir(wx) if "EVT_SIZ" in x]
['EVT_SIZE', 'wxEVT_SIZE']
Much faster than looking through the documentation (not that I have a
problem with the documentation - it's just that there's a lot of it).