Hey Folks, I’m trying to figure out how to get menu systems to enable/disable when an event happens but keep running into a road block.
In the below code is a simple menu, when the scripts start it shows a small frame with just 1 menu (YESNO), with 2 menu items in that menu (Start, Stop), initially Start is enabled and stop is disabled.
What I would like is that when I select start it disables the start menu item and enables the ‘stop’ menu item. same goes for when I click stop now that it is enabled.
The below code is what I have written so far and I was under the assumption that it would work but it gives me an error saying that m_stop is not defined, does anyone know what I am doing wrong?
Many thanks in advance!
~Leigh
Traceback (most recent call last):
File “menu-dev.py”, line 28, in StrStart
m_stop.Enable(True)
NameError: global name ‘m_stop’ is not defined
``
#!/usr/bin/env python
Import modules
import sys
import wx # wxPython
def main():
“”" Main entry point for the script."""
class MyForm(wx.Frame):
def init(self, parent, title):
wx.Frame.init(self, None, title=title, size=(100,50)) # Init the frame with a size of 100x50 pixels.
menuBar = wx.MenuBar()
Define the Menu.
s_menu = wx.Menu()
m_start = s_menu.Append(wx.ID_REDO, “&Start\tAlt-S”, “Start”)
m_stop = s_menu.Append(wx.ID_CLOSE, “S&top\tAlt-T”, “Stop”)
self.Bind(wx.EVT_MENU, self.StrStart, m_start)
self.Bind(wx.EVT_MENU, self.StrStop, m_stop)
menuBar.Append(s_menu, “&YESNO”)
m_stop.Enable(False)
self.SetMenuBar(menuBar)
self.Show(True)
def StrStart(self,event):
m_stop.Enable(True)
m_start.Enable(False)
def StrStop(self,event):
m_stop.Enable(False)
m_start.Enable(True)
app = wx.App(False)
frame = MyForm(None, ‘Menu Testing’)
app.MainLoop()
pass
if name == ‘main’:
sys.exit(main())
``