首页 > 解决方案 > wxpython:如何直接从菜单栏打开对话框

问题描述

我正在尝试找到一种方法来从 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。

我想节省点击菜单项,而不是在激活“关于菜单”时直接打开 AboutBox。

一个想法是从 wx.Menu 派生,但由于 wx.Menu 没有从 wx.Window 继承,所以我找不到要绑定的 Id。我的问题是:从事件的角度来看,当在菜单栏中单击一个条目(“关于菜单”)时会发生什么;然后打开一个菜单?那里有干涉的可能吗?还有什么想法吗?

PS:使用 python 3.8.0 和 wxPython 4.0.7.post2

标签: wxpython

解决方案


您可以选择,具体取决于您想要实现的目标。
使用菜单事件选项,您可以使用菜单的 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()

推荐阅读