我试图找到一种方法来从 MenuBar 打开 wx.Dialog 而不先点击 MenuEntry。
例子:
class Custom_MenuBar(wx.MenuBar):
def __init__(self, parent, style=0):
wx.MenuBar.__init__(self, style)
# About-Menu
aboutmenu = wx.Menu()
about_id = wx.Window.NewControlId()
aboutmenu.Append(about_id, "&About", "show About-Menu")
self.Bind(wx.EVT_MENU, self._about, id=about_id)
self.Append(aboutmenu, "About Menu")
# Menubar aktivieren
parent.SetMenuBar(self)
def _about(self, _event):
wx.adv.AboutBox(wx.adv.AboutDialogInfo())
此示例(以框架为父级)显示了一个带有“关于菜单”条目的菜单栏。单击此条目会显示一个名为“About”的 MenuItem,最后单击“About”会打开 AboutBox。
我想在菜单项上点击一下,而不是在激活“关于菜单”时直接打开关于框。
一个想法是从 wx.Menu 派生,但由于 wx.Menu 不是从 wx.Window 继承的,我没有找到要绑定到的 Id。我的问题是:当在 MenuBar 中单击条目(“关于菜单”)时,从事件的角度来看会发生什么?然后打开一个菜单?有干扰的可能吗?还有其他想法吗?
PS:使用 python 3.8.0 和 wxPython 4.0.7.post2
您有多种选择,具体取决于您想要实现的目标。
使用菜单事件选项,您可以使用菜单的 Id 或文本,如果没有 Id( menu open )。下面显示了最明显的选项。
import wx
import wx.stc
import wx.adv
class MyApp(wx.App):
def OnInit(self):
self.frame = MenuFrame(None, title="Menu Dialogue")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
class MenuFrame(wx.Frame):
def __init__(self, *args, **kwargs):
super(MenuFrame, self).__init__(*args, **kwargs)
# Attributes
self.panel = wx.Panel(self)
self.txtctrl = wx.stc.StyledTextCtrl(self.panel,
style=wx.TE_MULTILINE)
# Layout
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.txtctrl, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
self.CreateStatusBar() # For output display
# Setup the Menu
menu = wx.MenuBar()
# File Menu
filem = wx.Menu()
filem.Append(wx.ID_NEW, "New")
filem.Append(wx.ID_OPEN, "Open")
filem.Append(wx.ID_SAVE, "Save")
filem.Append(wx.ID_SAVEAS, "Save_As")
menu.Append(filem, "&File")
# About Menu
aboutm = wx.Menu()
self.about_id = wx.NewIdRef()
aboutm.Append(self.about_id, "&About Info", "Show About-Menu")
menu.Append(aboutm,"&About")
self.SetMenuBar(menu)
# Event Handlers
self.Bind(wx.EVT_MENU, self.OnMenu)
self.Bind(wx.EVT_MENU_OPEN, self.OnMenuOpen)
self.Bind(wx.EVT_MENU_HIGHLIGHT, self.OnMenuHighlight)
def OnMenu(self, event):
evt_id = event.GetId()
if evt_id == self.about_id:
info = wx.adv.AboutDialogInfo()
info.SetDescription("Menu Clicked "+str(evt_id)+" selected\n")
wx.adv.AboutBox(info)
def OnMenuOpen(self, event):
evt_id = event.GetId()
Dlg = False
if evt_id == 0:
obj = event.GetMenu()
if obj.GetTitle() == "&About":
Dlg = True
if Dlg:
info = wx.adv.AboutDialogInfo()
info.SetDescription("Menu Opened "+str(evt_id)+" selected\n")
wx.adv.AboutBox(info)
def OnMenuHighlight(self, event):
evt_id = event.GetId()
if evt_id == self.about_id:
info = wx.adv.AboutDialogInfo()
info.SetDescription("Menu Highlight "+str(evt_id)+" selected\n")
wx.adv.AboutBox(info)
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句