1
기본 프레임에 여러 하위 패널이있는 wx 응용 프로그램이 있습니다. 메인 프레임에는 각 메뉴가 패널과 연결된 메뉴 모음이 있습니다. 즉, 메뉴 항목을 만들고 이벤트 핸들러에 바인딩하는 작업은 메인 프레임이 아닌 개별 패널에서 수행해야합니다.wxPython : 하위 메뉴의 메뉴 채우기
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가 호출되지 않는다는 것을 지금 : 여기에 최소한의 예입니다. 이렇게하려면 깔끔한 방법이 있습니까?