wxpython:如何直接从菜单栏打开对话框
wxpython : how to open a Dialog directly from the MenuBar
我正在尝试找到一种方法来从 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 Menu" 的菜单栏。
单击此条目会显示名为 "About" 的 MenuItem,最后单击 "About" 打开 AboutBox。
我不想点击 MenuItem,而是在激活 "About Menu" 时直接打开 AboutBox。
一个想法是从 wx.Menu 派生的,但是由于 wx.Menu 没有继承自 wx.Window,我找不到要绑定到的 ID。
我的问题是:当在 MenuBar 中单击条目 ("About Menu") 时,从事件的角度来看会发生什么;然后打开菜单?那里有可能干涉吗?还有其他想法吗?
PS: 使用 python 3.8.0 和 wxPython 4.0.7.post2
您有多种选择,具体取决于您想要实现的目标。
使用菜单事件选项,您可以使用菜单的 Id 或文本(如果没有 Id(菜单打开))。
以下显示了最明显的选项。
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()
我正在尝试找到一种方法来从 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 Menu" 的菜单栏。 单击此条目会显示名为 "About" 的 MenuItem,最后单击 "About" 打开 AboutBox。
我不想点击 MenuItem,而是在激活 "About Menu" 时直接打开 AboutBox。
一个想法是从 wx.Menu 派生的,但是由于 wx.Menu 没有继承自 wx.Window,我找不到要绑定到的 ID。 我的问题是:当在 MenuBar 中单击条目 ("About Menu") 时,从事件的角度来看会发生什么;然后打开菜单?那里有可能干涉吗?还有其他想法吗?
PS: 使用 python 3.8.0 和 wxPython 4.0.7.post2
您有多种选择,具体取决于您想要实现的目标。
使用菜单事件选项,您可以使用菜单的 Id 或文本(如果没有 Id(菜单打开))。
以下显示了最明显的选项。
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()