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!