Create a new import

HI,

I'm trying to create a class that I can import into my apps that
creates the usual menus that go with most app - i.e file, edit, save ,
about etc but I'm not too sure what I should be passing in to it.

I'm using code from the wxPython in Action book to ensure all the
syntax is correct.

So I have a file called Menus.py that contains this class:

import wx

class test():

    def menuData(self):
        return [("&File", (
                    ("&New", "New file", self.OnNew),
                    ("&Open", "Open file", self.OnOpen),
                    ("&Save", "Save file", self.OnSave),
                    ("", "", ""),
                    ("About...", "Show about window", self.OnAbout),
                    ("&Quit", "Quit", self.OnCloseWindow)))]

    def createMenuBar(self):
        menuBar = wx.MenuBar()
        for eachMenuData in self.menuData():
            menuLabel = eachMenuData[0]
            menuItems = eachMenuData[1]
            menuBar.Append(self.createMenu(menuItems), menuLabel)
        self.SetMenuBar(menuBar)

    def createMenu(self, menuData):
        menu = wx.Menu()
        for eachItem in menuData:
            if len(eachItem) == 2:
                label = eachItem[0]
                subMenu = self.createMenu(eachItem[1])
                menu.AppendMenu(wx.NewId(), label, subMenu)
            else:
                self.createMenuItem(menu, *eachItem)
        return menu

(the code is stright out of the book so should be correct)

Then in my main app I try to import it via the __init__ of the class
(bottom two lines):

from Menus import test

class SketchFrame(wx.Frame):

    def __init__(self, parent):
        self.title = "Sketch Frame"
        wx.Frame.__init__(self, parent, -1, self.title,
                size=(800,600))
        self.filename = ""
        self.sketch = SketchWindow(self, -1)
        self.sketch.SetMinSize((10,10))
        wx.EVT_MOTION(self.sketch, self.OnSketchMotion)
        self.initStatusBar()
        self.createToolBar()
        self.menus = test(self)
        self.menus.createMenuBar()

The idea obviously being to eventually add statusbar(), toolbar() etc
to this module to be make a single call to create them and be able to
use it for all app's and not have to type it out each time (the code
seems pretty standard and reusable to me).

The problem is that the handlers are not yet defined so when "("&New",
"New file", self.OnNew)," is encountered, it doesn't know what it is
so fails with:

   self.OnNew = OnNew()
NameError: global name 'OnNew' is not defined

I understand why the error is occurring but not too sure how to fix
it. Do I need to pass any arguments in for super classes within wx for
it too?

Would you all say that this would be the best way to create a module
to create the menus for me, or is it better to just add it manually
each time?

Thanks for any help,

John

···

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en