1

我在使用菜单系统时遇到问题 - 我在此处(如下)有一个基本示例,显示了我遵循的基本菜单示例;具体来说,我的问题是如何从使用菜单选择中做出决定,我不确定如何将用户选择与菜单选择进行交互?有人能指出我正确的方向吗,或者最好给出一个简短的例子——比如从菜单输入数据并显示这个?

谢谢

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

1 回答 1

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()
于 2013-01-28T12:11:09.973 回答