import wx

class McaMenu(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(640, 432))

menubar = wx.MenuBar()
file = wx.Menu()
new = wx.MenuItem(file, 1, '&New\tCtrl+N', 'Creates a new document')
file.AppendItem(new)
file.AppendSeparator()


open = wx.MenuItem(file, 2, '&Open\tCtrl+O', 'Open New document')
file.AppendItem(open)


save = wx.MenuItem(file, 3, '&Save\tCtrl+S', 'Save')
file.AppendItem(save)


save = wx.MenuItem(file, 4, '&Save As...', 'Save As...')
file.AppendItem(save)
file.AppendSeparator()


Print = wx.MenuItem(file, 5, '&Print', 'Print')
file.AppendItem(Print)


PrintPreview = wx.MenuItem(file, 6, '&Print Preview', 'Print Preview')
file.AppendItem(PrintPreview)


PrintSetup = wx.MenuItem(file, 7, '&Print Setup', 'Print Setup')
file.AppendItem(PrintSetup)


RecentFile = wx.MenuItem(file, 8, '&Recent File', 'Recent File')
file.AppendItem(RecentFile)
file.AppendSeparator()


quit = wx.MenuItem(file, 9, '&Quit\tCtrl+Q')
file.AppendItem(quit)
self.Bind(wx.EVT_MENU, self.OnQuit, id=9)

menubar.Append(file, '&File')
self.SetMenuBar(menubar)

Edit = wx.Menu()
Edit.Append(1, 'Selection', 'Quit application')
Edit.Append(2, 'Cut', 'Quit application')
Edit.Append(3, 'Copy', 'Quit application')
Edit.Append(4, 'Paste', 'Quit application')
menubar.Append(Edit, '&Edit')
self.SetMenuBar(menubar)

View = wx.Menu()
###the python gives on this line and held my system###
self.shtl = view.Append(1, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)
view.Check(1, True)

self.Bind(wx.EVT_MENU, self.ToggleToolBar, id=1)

self.toolbar = self.CreateToolBar()
self.toolbar.Realize()

def ToggleToolBar(self, event):
if self.shtl.IsChecked():
self.toolbar.Show()
else:
self.toolbar.Hide()
self.Centre()
self.Show(True)

self.shst = view.Append(2, 'Status Bar', 'Status Bar', kind=wx.ITEM_CHECK)
view.Check(2, True)
self.Bind(wx.EVT_MENU, self.ToggleStatusBar, id=2)


def ToggleStatusBar(self, event):
if self.shst.IsChecked():
self.statusbar.Show()
else:
self.statusbar.Hide()
self.Centre()
self.Show(True)
View.Append(3, 'Delete Rows', 'Quit application')
menubar.Append(View, '&View')
self.SetMenuBar(menubar)

Graph = wx.Menu()
Graph.Append(1, 'Adjust Plot', 'Quit application')
Graph.Append(2, 'Scatter Plot', 'Quit application')
Graph.Append(3, 'Predict Plot', 'Quit application')
Graph.Append(4, 'Fitted vs Residual', 'Quit application')
Graph.Append(5, 'Variable vs Residual', 'Quit application')
Graph.Append(6, 'Ext Std Residual vs Fitted', 'Quit application')
menubar.Append(Graph, '&Graph')
self.SetMenuBar(menubar)

Data = wx.Menu()
Data.Append(1, 'Settings', 'Quit application')
Data.Append(2, 'Summary', 'Quit application')
Data.Append(3, 'Row Operation', 'Quit application')
Data.AppendSeparator()

Rowoperation = wx.Menu()
Rowoperation.Append(1, 'Add')
Rowoperation.Append(1, 'Delete')
Rowoperation.Append(1, 'Undelete All')

Data.AppendMenu(1, '&Rowoperation', Rowoperation)


menubar.Append(Data, '&Data')
self.SetMenuBar(menubar)

Variable = wx.Menu()
Variable.Append(1, 'Destroy', 'Quit application')
Variable.Append(2, 'Interactioin', 'Quit application')
Variable.Append(3, 'Transformation', 'Quit application')
menubar.Append(Variable, '&Variable')
self.SetMenuBar(menubar)

Correlation = wx.Menu()
Correlation.Append(1, 'Matrix', 'Quit application')
Correlation.Append(2, 'New Model', 'Quit application')
Correlation.Append(3, 'Destroy Model', 'Quit application')
Correlation.Append(4, 'Previous Model', 'Quit application')
menubar.Append(Correlation, '&Correlation')
self.SetMenuBar(menubar)

Prediction = wx.Menu()
Prediction.Append(1, 'Input Sheet', 'Quit application')
Prediction.Append(2, 'Result Sheet', 'Quit application')
Prediction.Append(3, 'Clear Input Sheet', 'Quit application')
menubar.Append(Prediction, '&Prediction')
self.SetMenuBar(menubar)

Reseduals = wx.Menu()
Reseduals.Append(1, 'All', 'Quit application')
Reseduals.Append(2, 'Delete Rows', 'Quit application')
Reseduals.Append(3, 'Included Rows', 'Quit application')
Reseduals.Append(4, '1st Outlier table', 'Quit application')
Reseduals.Append(5, '2nd Outlier table', 'Quit application')
menubar.Append(Reseduals, '&Reseduals')
self.SetMenuBar(menubar)

Help = wx.Menu()
Help.Append(1, 'Help Topics', 'Quit application')
Help.Append(2, 'About MCA', 'Quit application')
menubar.Append(Help, '&Help')
self.SetMenuBar(menubar)

self.Centre()
self.Show(True)

def OnQuit(self, event):
self.Close()

app = wx.App()
McaMenu(None, -1, 'MCA menu')
app.MainLoop()

kindly check this code and explain the possible error that i am making.
Regards
Punter

Dani AI

Generated

The immediate problem is a simple Python gotcha that already hinted at: Python is case sensitive. If you do View = wx.Menu() and later call view.Append(...), view is undefined and the program will raise a NameError (run the script from a terminal to see the traceback). Fix that first by using the same name consistently (for example view_menu = wx.Menu() and then view_menu.Append...).

Other things to check while fixing the menu code:

  • Indentation and scoping: make sure all menu creation and self.* assignments are inside __init__ (indented under the method). If code that uses self is outside the class or method it will fail.
  • Create toolbar/statusbar before toggling them. self.toolbar = self.CreateToolBar() and self.statusbar = self.CreateStatusBar() must exist before handlers call Show()/Hide().
  • Prefer AppendCheckItem (or AppendCheckItem API for your wx version) for checkbox menu items instead of manually using kind=wx.ITEM_CHECK. It returns a menu item you can store on self.
  • Use unique IDs (or wx.ID_ANY) rather than reusing literal numbers across menus; duplicate IDs can make event routing confusing.
  • Bind by the returned menu-item id (or by storing the item on self) so handlers operate on the correct item. After show/hide, call self.Layout() to update window layout instead of repeatedly calling Centre()/Show().

Minimal pattern to follow (adapt to your names and wx version):

view_menu = wx.Menu()
self.show_toolbar_item = view_menu.AppendCheckItem(wx.ID_ANY, "Show toolbar")
menubar.Append(view_menu, "&View")
self.SetMenuBar(menubar)

self.Bind(wx.EVT_MENU, self.OnToggleToolbar, id=self.show_toolbar_item.GetId())

def OnToggleToolbar(self, event):
    if self.show_toolbar_item.IsChecked():
        self.toolbar.Show()
    else:
        self.toolbar.Hide()
    self.Layout()

See the wx.Menu docs for the available Append methods and menu-item behavior: wx.Menu. Also follow ’s advice and post properly formatted code (use code tags) so indentation and exact errors are visible.

Recommended Answers

All 2 Replies

If you use code tags, I will take a look at your code.

Please use the [code] and [/code] tag pair to enclose your code. This way the proper indentations will show, making your code readable.

Put your code in code tags, and try to boil it down to what is going wrong a little.

Your 'View' and 'view' should be the same caps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.