Newbie question re:wxMenuBar events

In the wxGlade dialog for editing menu items there is an “id” field for items. I had left it blank and I believe that’s why each item had an id of “-1” (I think this prompts the code to auto-assign some id to these objects). On the guess that this “id” field might actually name that menu choice (i.e. you might have a button called enable_ssl_button that kicks off a function called enable_ssl), I entered an id (“close_cw_menu_item”) for the close_cw menu item. Then when I generate code, running it returns a squawk "NameError: global name ‘close_cw_menu_item’ is not defined…

···

---------- Forwarded message ----------
From: Christopher Barker Chris.Barker@noaa.gov

To: wxpython-users@lists.wxwidgets.org
Date: Wed, 07 May 2008 11:58:44 -0700
Subject: Re: [wxpython-users] Newbie question re:wxMenuBar events
Gre7g Luterman wrote:

if I were writing this code by hand, each item would look more like:
ID_BUY = wx.NewId()
wxglade_tmp_menu.Append(ID_BUY, “Buy CW”, “”, wx.ITEM_NORMAL)

self.Bind(wx.EVT_MENU, self.buy_license, id=ID_BUY)

I’d do:

item = wxglade_tmp_menu.Append(wx.ID_ANY, “Buy CW”, “”, wx.ITEM_NORMAL)
self.Bind(wx.EVT_MENU, self.buy_license, item)

see:
http://wiki.wxpython.org/wxPython%20Style%20Guide

I really don’t like IDs. I do wish you could just do:

Menu_Item.Bind(…)

But such is life.

-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@noaa.gov


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Randy Rue wrote:

In the wxGlade dialog for editing menu items there is an "id" field for items. I had left it blank and I believe that's why each item had an id of "-1" (I think this prompts the code to auto-assign some id to these objects). On the guess that this "id" field might actually name that menu choice (i.e. you might have a button called enable_ssl_button that kicks off a function called enable_ssl), I entered an id ("close_cw_menu_item") for the close_cw menu item. Then when I generate code, running it returns a squawk "NameError: global name 'close_cw_menu_item' is not defined....

The "-1" is pretty much equivalent to wx.ID_ANY, which means what you say, namely that wx will "randomly" choose an unused id for the control and manage the ids in such a way that there won't be any collisions. You can specify you're own ids, but they need to be above some number, which I currently do not recall.

However, looking at the WIA book, it seems you can use wx.RegisterId() to prevent wx from using your explicitly assigned id. The book also mentions that you should not use ID numbers between wx.ID_LOWEST and wx.ID_HIGHEST. Kind of vague, but that should help you some.

As for the error you get, that would be caused because you didn't define "close_cw_menu_item" earlier in the code. If you had done this:

close_cw_menu_item = wx.ID_ANY # or 12345 or some other number

then it would have worked.

Mike

<snip>