显示每个菜单的不同面板使用python库wxpython

问题描述 投票:0回答:1
Python代码显示带有菜单按钮的Menubar

import wx #dashboard frame class mainGUI(wx.Frame): def __init__(self,parent,title): wx.Frame.__init__(self,parent,title=title,size=(1024,780)) self.initialise() def initialise(self): panel=wx.Panel(self) menubar=wx.MenuBar() #buttons for menu home=wx.Menu() report=wx.Menu() statics=wx.Menu() data=wx.Menu() chart=wx.Menu() #appending button to the menubar #here should be menu event handler for each panel to show menubar.Append(home,"Home") menubar.Append(report,"report") menubar.Append(statics,"statics") menubar.Append(data,"data") menubar.Append(chart,"chart") self.SetMenuBar(menubar) Classes should be here for each panel #Attaching the event handler for each menu self.Show(True)

	
python wxpython
1个回答
0
投票

self.Bind(wx.EVT_MENU_OPEN, self.OnMenu, menubar)

将触发onMenu方法。

使用方法,您可以识别出使用事件查询的菜单:

evt.Menu.Title

从那里您可以更改当前显示的面板。
import wx class MainGUI(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title, size=(1024, 780)) self.initialise() def initialise(self): menubar = wx.MenuBar() home = wx.Menu() report = wx.Menu() menubar.Append(home, "Home") menubar.Append(report, "report") self.Bind(wx.EVT_MENU_OPEN, self.OnMenu, menubar) self.SetMenuBar(menubar) def OnMenu(self, evt=None): if evt.Menu.Title == 'Home': print('Home menu clicked') elif evt.Menu.Title == 'report': print('report menu clicked') class MainApp(wx.App): def __init__(self): wx.App.__init__(self) main_window = MainGUI(None, title='My app') main_window.Show() def main(): app = MainApp() app.MainLoop() if __name__ == '__main__': main()


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.