wxpython - 菜单交互

时间:2013-01-27 21:56:42

标签: menu wxpython

我在菜单系统方面遇到了问题 - 我在这里有一个基本的例子(下图),它显示了我所遵循的基本菜单示例;具体来说,我的问题是如何根据使用菜单选择做出决定,我不确定如何将用户选择与菜单选项进行交互? 有人能指出我正确的方向,或者理想情况下给出一个简单的例子 - 从菜单中输入数据并显示它?

谢谢

import wx

class myFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,'Menu', size=(300,200))
        panel = wx.Panel(self)

        status     = self.CreateStatusBar()
        menubar    = wx.MenuBar()
        firstMenu  = wx.Menu() 
        secondMenu = wx.Menu()


        # create files
        firstMenu.Append(wx.NewId(), 'Save Data'  , 'Save data')        
        firstMenu.Append(wx.NewId(), 'Open Data..', 'Open a new window') 
        secondMenu.Append(wx.NewId(),'Configure..', 'Input Data here')
        # append to menu
        menubar.Append(firstMenu, 'File')
        menubar.Append(secondMenu,'Options')
        # 
        self.SetMenuBar(menubar)

if( __name__ == '__main__' ):
    app = wx.PySimpleApp()
    frame = myFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:2)

您必须绑定wx.EVT_MENU事件。有关更多示例,请参阅wxPython演示。在你的情况下,这将是一些想法:

import wx

SAVE_DATA = wx.NewId()
OPEN_DATA = wx.NewId()
CONFIGURE = wx.NewId()

class myFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,'Menu', size=(300,200))
        panel = wx.Panel(self)

        status     = self.CreateStatusBar()
        menubar    = wx.MenuBar()
        firstMenu  = wx.Menu() 
        secondMenu = wx.Menu()

        # create files
        firstMenu.Append(SAVE_DATA, 'Save Data'  , 'Save data')        
        firstMenu.Append(OPEN_DATA, 'Open Data..', 'Open a new window') 
        secondMenu.Append(CONFIGURE,'Configure..', 'Input Data here')
        # append to menu
        menubar.Append(firstMenu, 'File')
        menubar.Append(secondMenu,'Options')
        # 
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.SaveData, id=SAVE_DATA)
        self.Bind(wx.EVT_MENU, self.OpenData, id=OPEN_DATA)
        self.Bind(wx.EVT_MENU, self.Configure, id=CONFIGURE)

    def SaveData(self, e):
        print("Save")

    def OpenData(self, e):
        print("Open")

    def Configure(self, e):
        print("Config")

if( __name__ == '__main__' ):
    app = wx.PySimpleApp()
    frame = myFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()