[wxPython-users] how to automate menu generation ?

Christopher Barker Chris.Barker at noaa.gov
Tue Nov 6 22:38:20 PST 2007


Stef Mientki wrote:
> I want to automate the generation of my form menu:

good idea -- I think generating GUIS in code is often an excellent 
solution. Peter gave you a sample, but I have a few comments:

> def generate_menu ( form, menus ) :
>    # Prepare the menu bar
>    menuBar = wx.MenuBar()
> 
>    for menu in menus:
>      menu_top = wx.Menu ()
>      for item in menus [ menu ] :
>        menu_item = menu_top.Append ( wx.ID_ANY, item[0] )
>        line = 'ID_' + item[1] + ' = menu_item.GetId()'
>        exec line

I'd recommend against this. exec is ugly and dangerous, and hardly ever 
(never? ) necessary in Python. And why do you need to store an ID like 
that? You probably don't need to store the ID at all, but if you do, 
store it in a dict.

> Now I have 2 problems:
> 1- where the arrow points, generates an error, because the method 
> "self.OnMenu_..."  doesn't yet exists
> Of course I need to create it myself sometimes, but for the moment I 
> want a dummy procedure,

What do you mean -- for the moment? of you mean while you are testing 
the code, then it's easy to create one method, and bind all the menu 
events to that. If you want to have a dummy method when this code runs, 
and fill it in with the real one later, then I think you'll need to run 
the binding code later, when you want the menus bound (or re-bound)

> which will gray the related menu item.
> 2- how can I create the above mentioned dummy procedure, so it will 
> become a normal method of the form ?

You can add methods to a class object later with setattr()

> Maybe I'm asking too much,
> but I have the feeling that these kinds of automations (I've few others 
> in my head),
> should be possible in Python.

Those and more -- Python is wonderful for this kind of thing.

-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