[wxPython-users] Re: How to know what event IDs to use in menu?

Christopher Barker Chris.Barker at noaa.gov
Wed Apr 25 11:41:12 PDT 2007


Thanks for the long clarifications. From struggling with getting going 
to this in a very short time -- nice going!

>> When would you use
>> wx.ID_ABOUT or wx.ID_EXIT?  
> 
> I think the underlying operating system applies some default
> actions to widgets with those id's, so if you want those default actions 
> to apply, you would use those id's.  

exactly.

> As another example, on a Mac, when I try using the id wx.ID_EXIT, 
> like this:
> 
> class MyFrame(wx.Frame):
>     def __init__(self):
>         wx.Frame.__init__(self, None)
>         
>         mbar = wx.MenuBar()
>         menu = wx.Menu()
>         menu.Append(wx.ID_EXIT, "Exit...")
>         mbar.Append(menu, "Test") 
>        
>         self.SetMenuBar(mbar)
> 
> it doesn't work at all: no menu item with the name "Exit..." appears 
> anywhere.  Apparently, a Mac won't let me you use that id.

It uses the "Quit" menuitem in the Application menu, so what you bind to 
it will get called.

> if I set the text to exactly "Exit", then "Exit" is automatically 
> changed to "Quit".  Therefore, a Mac won't allow me to use the id 
> wx_ID_EXIT nor the name "Exit" for my menu items.

either the ID or the Exit name gets caught and shifted to the 
appropriate place on a Mac. See:

http://wiki.wxpython.org/index.cgi/Optimizing_for_Mac_OS_X

For more detail.


> self.Bind(wx.EVT_BUTTON, self.someFunc, *panel.button*)

better yet:

*panel.button*.Bind(wx.EVT_BUTTON, self.someFunc)

> As I see it, a better use case for id's is using the Bind() method's 
> "id" and "id2" parameters together.  When used together, they 
> specify a range of widget id's that the event handler should 
> apply to. That could save you from having to type, say, 10 Bind() 
> statements to apply one event handler to 10 different widgets.

this is the ONLY time I think it's a good idea to use ids, though I then 
to still not do it that way, and rather put the objects in a Python 
sequence:

for b in ListOfButtons:
     b.Bind(wx.EVT_BUTTON, self.someFunc)

look ma! no ids! and only one Bind() statement.

> I'm new to wxPython so I don't know if specifying id's is done 
> much in practice. 

A couple years ago, it was done a lot.

> So, it would be helpful if some experienced 
> people posted the circumstances under which they specify their own 
> id's in a program.

Now most folks are shifting to a style that doesn't use ids. I think a 
couple of "old hands" do use them in the way described -- when you have 
a range that's meaningful, or if you want a MenuItem and toolbar button 
to share the same callback.

> As I understand things, it's worse than that.  In Windows, the 
> lol.  Doing Windows GUI programming directly with the C api is 
> pretty difficult. Using C++ classes that wrap the C code makes 
> things easier but it is still difficult--even with a good IDE--and 
> it presents its own mysteries.  Compared to those options, 
> specifying -1 for an id when using wxPython is a breeze.

So true! but we're spoiled with Python here, and wxPython has gotten 
more Pythonic over the years too.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list