[wxpython-users] dynamic vs. static event binding in a custom popup
menu class
Tim Black
timblack0 at gmail.com
Tue Apr 1 23:40:50 PDT 2008
I have created a custom class that my windows can use for popup menus. Usage is
as follows: the window constructs a PopupMenu object, passing its constructor a
recursive list of simple MenuItem objects (not wx.MenuItems) that represent the
attributes and bindings of each menu item (text, is_checkable, etc...). The
PopupMenu class constructor also gets a reference to the window, so it can do
the event binding as well as the menu creation.
I have implemented and tested the event binding in the PopupMenu class two
different ways, and I'm looking for advice on which is best.
0. Each MenuItem gets a dynamically acquired id from wx.NewId() that is used for
the life of that popup menu. Menu events are bound using this id and the window
reference. Because this all happens in the PopupMenu constructor, the window
doesn't have to know anything about menu ids.
1. The MenuItems get assigned static ids by the window before passing to the
PopupMenu constructor. Menu events are bound using these ids the first time the
popup menu is created. Since the ids are constants, each subsequent time the
same popup menu is created, the events do not need to be bound.
It seems to me that the negative performance impact of binding each time the
popup is created would be minimal, and that the benefit gained from
encapsulating all menu-related stuff in the PopupMenu class outweighs this. The
possible downside to 0. is that I don't know how to unbind events each time the
popup menu is destroyed so that I don't get a leak in the window's event handler
table.
Usage is like the following:
def OnContextMenu(self, event):
...
popup = PopupMenu(self, menu_item_list)
self.PopupMenu(popup)
The PopUpMenu constructor does the binding. wx.Window.PopupMenu draws the menu
and doesn't return until the user has dismissed it. Can I define a destructor
__del__ for PopupMenu and do the unbinding in there? To prevent the event
handler table from growing, would it be sufficient to do this and then
explicitly call
del popup
afterwards?
Thoughts?
Thanks,
Tim
More information about the wxpython-users
mailing list