menu IDs

Hi

I have a menu defined in a frame and need to enable/disable some of the menu items as the user adds/deletes data while the program is executing. The code snippet is as follows:

class TopFrame(wx.Frame):
     """Frame class that acts as top level window"""

     # Constructor for main window

     def __init__(self, data):

         self.data = data

         # Create a Frame instance

         wx.Frame.__init__(self, None, title='new model')

         # Create the menu bar

         menuBar = wx.MenuBar()

         menuFile = wx.Menu()
         file_new = menuFile.Append(wx.ID_NEW, "&New case...")
         file_open = menuFile.Append(wx.ID_OPEN, "&Open existing case...")
         file_save = menuFile.Append(wx.ID_SAVE, "&Save current case...")
         file_exit = menuFile.Append(wx.ID_EXIT, "E&xit")

         menuEdit = wx.Menu()
         edit_town = menuEdit.Append(100000, "&Towns...")
         edit_city = menuEdit.Append(100001, "&Cities...")

         menuCalc = wx.Menu()
         do_calcs = menuEdit.Append(110000, "&Calculate")

         menuBar.Append(menuFile,"&File")
         menuBar.Append(menuEdit,"&Edit")
         menuBar.Append(menuCalc,"&Calcs")

         self.SetMenuBar(menuBar)

         # Refresh the menu settings

         self.refreshMenu()

     # Enable/disable menu settings depending on the model status

     def refreshMenu(self):

         # Get the number of items in the model

         ncity = len(self.data.cities)
         ntown = len(self.data.towns)

         # Get the menu items

         edit_town = self.FindWindowById(100000)
         edit_city = self.FindWindowById(100001)

         do_calcs = self.FindWindowById(110000)

         # Set the menu status

         if ncity > 0 :
             edit_city.Disable()
         else:
             edit_city.Enable()

         if ntown > 0:
             edit_town.Enable()
         else:
             edit_town.Disable()

         if ntown * ncity > 0:
             do_calcs.Enable()
         else:
             do_calcs.Disable()

I thought that setting the menu item IDs explicitly would allow me to "get at" them later on using FindItemById(). However, Python gives the error

'NoneType' object has no attribute 'Disable'

when it meets the line edit_city.Disable() so I guess that means that it has not been able to find the window whose ID is 100001 (ie "edit city" menu item) the even though I set this when TopFrame was constructed. Hmm.... I had a look at the WXWIDGETS website and found that wxMenuItem is not derived from wxWindows which I guess is the reason why I'm getting the error.

What I would like to do is something like

edit_town = self.FindMenuitemById(100000)

which would return a wxMenuItem that I can then enable/disable. Thanks for your help!

Hi Alun,

I have a menu defined in a frame and need to enable/disable some of the
menu items as the user adds/deletes data while the program is
executing. The code snippet is as follows:

You could try something like this (untested!!):

def refreshMenu(self):

    edit_id = self.GetMenuBar().FindMenu("Edit")
    edit_menu = self.GetMenuBar().GetMenu(edit_id)

    edit_town = edit_menu.FindItem("Towns...")
    edit_cities = edit_menu.FindItem("Cities...")

    # Do whatever you want with those menu, such as:
    edit_menu.Enable(edit_town, False)

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

I try to avoid IDs everywhere -- it just seems clunky to me. Why not just keep a reference to the menu item, rather than its ID:

         menuEdit = wx.Menu()
         self.edit_town = menuEdit.Append(wx.ID_ANY, text="&Towns...")
         self.edit_city = menuEdit.Append(wx.ID_ANY, text="&Cities...")

then:

     def refreshMenu(self):

  ...
         edit_town = self.edit_town
         edit_city = self.edit_city
         ...
         if ncity > 0 :
             edit_city.Enable(False)
         else:
             edit_city.Enable(True)

Enclosed is a working version.

By the way. In the future, please try to post a complete, self contained, working (as much as you can) sample app. That way we can tweak it and test it with little effort.

-Chris

MenuDisable.py (2.01 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (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