【发布时间】:2017-10-13 07:55:36
【问题描述】:
我有一个 wx 应用程序,其中主框架有几个子面板。我想在主框架中有一个菜单栏,其中每个菜单都与一个面板相关联。这意味着菜单项的创建并将它们绑定到事件处理程序应该在各个面板中完成,而不是在主框架中完成。这是一个最小的例子:
import wx
class myPanel1(wx.Panel):
def __init__(self, parent, menubar):
super().__init__(parent=parent)
menu = wx.Menu()
menuAction1 = menu.Append(wx.ID_ANY, 'Action1')
menuAction2 = menu.Append(wx.ID_ANY, 'Action2')
menubar.Append(menu, '&Actions')
# This does not work because the EVT_MENU is only seen by the main frame(?)
self.Bind(wx.EVT_MENU, self.onAction1, menuAction1)
self.Bind(wx.EVT_MENU, self.onAction2, menuAction2)
def onAction1(self, event):
print('Hello1')
def onAction2(self, event):
print('Hello2')
class mainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.menubar = wx.MenuBar()
# There are more panels in my actual program
self.panel1 = myPanel1(self, self.menubar)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.panel1, flag=wx.EXPAND, proportion=1)
self.SetSizerAndFit(sizer)
self.SetMenuBar(self.menubar)
self.Layout()
class myApp(wx.App):
def OnInit(self):
frame = mainWindow(parent=None, title='Title')
self.SetTopWindow(frame)
frame.Show()
return True
if __name__ == '__main__':
app = myApp()
app.MainLoop()
现在的问题是 myPanel1.onAction1 没有被调用,因为来自主框架的菜单事件没有传播到子面板。 有什么好办法吗?
【问题讨论】:
标签: python event-handling wxpython