wxPython menu creation refactoring

alex alecla at bluewin.ch
Tue Apr 28 22:49:43 CEST 2009


Hello everybody
I am still trying to refactor a simple GUI basing on an example
in "wxPython In Action", "Listing 5.5 A refactored example" where the
menue creation is "automatized".
I understand the problem (each second for loop in "def createMenuData
(self)"
creates a distinct menu) but I tried now for some evenings and do not
get to
any possible solution.
I am still learning Python and hope to use it as a front end for
my Fortran programs (subprocess and ctypes work very well...)
with the aim of increasing the Python part (text parsing) in the
future.
The following code shows the problem and works out of the DOS box.
Any help would be greatly apreciated.

Alex


#
#!/usr/bin/env python
#
"""Add Python docs string"""
import wx
#
#
class Frame(wx.Frame):
    def __init__(self, title, pos, size):
        wx.Frame.__init__(self, None, -1, title, pos, size)
#
        self.createPanel()
        self.createMenuData()


    def menuData(self):
        return (("&File",
                ("&Open", self.OnOpen),
                ("&Restart", self.OnRestart),
                ("&Exit", self.OnExit)),
                ("&About",
                ("&About", self.OnAbout)))


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


    def createMenu(self, itemLabel, itemHandler):
        menu = wx.Menu()
        menuItem = menu.Append(wx.NewId(), itemLabel)
        self.Bind(wx.EVT_MENU, itemHandler, menuItem)
        return menu


    def createPanel(self):
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(wx.SystemSettings_GetColour
(wx.SYS_COLOUR_3DFACE))
        StatusBar = self.CreateStatusBar()

        self.Centre()
        self.Show(True)


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

    def OnOpen(self, event):
        pass

    def OnRestart(self, event):
        pass

    def OnAbout(self, event):
        pass


class App(wx.App):
    def OnInit(self):
        frame = Frame("Gui Frame", (100, 100), (600, 450))
        frame.Show()
        self.SetTopWindow(frame)
        return True
#
#
if __name__ == '__main__':
#
    app = App()
    app.MainLoop()
#
#



More information about the Python-list mailing list