Enabling/Disabing a wx.Menu from an event.

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())

``

Resolved, apologies for posting to the list in apparent haste when I should have just looked and thought about it harder.

Issue was for anyone in the future running into the same issue was that I was not adding ‘self.’ in front of the m_start and m_stop, this meant I was not able to call it in anything other than the main def init function, adding the ‘self.’ means I can call it from other functions (there is probably a better explanation than but that is how I am able to explain it in my mind), code block below that works for anyone interested.

#!/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()

self.m_start = s_menu.Append(wx.ID_REDO, “&Start\tAlt-S”, “Start”)

self.m_stop = s_menu.Append(wx.ID_CLOSE, “S&top\tAlt-T”, “Stop”)

self.Bind(wx.EVT_MENU, self.StrStart, self.m_start)

self.Bind(wx.EVT_MENU, self.StrStop, self.m_stop)

menuBar.Append(s_menu, “&YESNO”)

self.m_stop.Enable(False)

self.SetMenuBar(menuBar)

self.Show(True)

def StrStart(self,event):

self.m_stop.Enable(True)

self.m_start.Enable(False)

def StrStop(self,event):

self.m_stop.Enable(False)

self.m_start.Enable(True)

app = wx.App(False)

frame = MyForm(None, ‘Menu Testing’)

app.MainLoop()

pass

if name == ‘main’:

sys.exit(main())

``

···

On Sunday, September 20, 2015 at 2:29:05 PM UTC-7, Leighb2282 wrote:

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