在 wxpython 中更改多字段状态栏中的默认字段(以显示菜单项帮助文本)

Change default field in a multi field statusbar in wxpython (to display menuitem help text)

在wxpython中,当我们创建多字段状态栏时,默认使用第一个字段,显示鼠标悬停菜单项的帮助文本。

例如下面的代码:

import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Multi field status bar.')
        panel = wx.Panel(self)

        self.statusbar = self.CreateStatusBar(2)
        self.menubar = wx.MenuBar()
        self.menuitem = wx.Menu()
        self.SetMenuBar(self.menubar)

        if True:  # Utilitie IDs:
            self.ID_new_utilitie = wx.NewId()
            self.ID_delete_utilitie = wx.NewId()

        if True:  # Menu items:
            self.menuitem.Append(self.ID_new_utilitie, "&New", "Create New Utilitie.")
            self.menuitem.Append(self.ID_delete_utilitie, "&Delete", "Delete Selected Utilitie.")
            self.menubar.Append(self.menuitem, '&Actions')

        if True:  # Utilitie Actions:
            wx.EVT_MENU(self, self.ID_new_utilitie, self.Create_New_Utilitie)
            wx.EVT_MENU(self, self.ID_delete_utilitie, self.Delete_Selected_Utilitie)

        self.Show()

    def Create_New_Utilitie(self, event):
        pass

    def Delete_Selected_Utilitie(self, event):
        pass


if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

每当我们将鼠标悬停在菜单项上时,帮助文本会自动显示在第一个字段中(默认情况下)。

An image illustrating my question while running the example above.

请注意,我不是在询问如何设置带有帮助文本的第二个字段的状态文本,而是在询问如何更改显示它们的默认字段。

谢谢。

好的,我终于找到答案了

要更改状态栏中的默认字段(默认情况下会显示菜单帮助字符串),您需要使用 EVT_MENU_HIGHLIGHT_ALL(func).

所以以我上面问题中的代码为例,这里是变化:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Multi field status bar.')
        panel = wx.Panel(self)

        self.statusbar = self.CreateStatusBar(2)
        self.menubar = wx.MenuBar()
        self.menuitem = wx.Menu()
        self.SetMenuBar(self.menubar)

        self.statusbar.SetStatusText('Nothing here!!!', 0)

        if True:  # Utilitie IDs:
            self.ID_new_utilitie = wx.NewId()
            self.ID_delete_utilitie = wx.NewId()

        if True:  # Menu items:
            self.menuitem.Append(self.ID_new_utilitie, "&New", "Create New Utilitie.")
            self.menuitem.Append(self.ID_delete_utilitie, "&Delete", "Delete Selected Utilitie.")
            self.menubar.Append(self.menuitem, '&Actions')

        if True:  # Utilitie Actions:
            wx.EVT_MENU(self, self.ID_new_utilitie, self.Create_New_Utilitie)
            wx.EVT_MENU(self, self.ID_delete_utilitie, self.Delete_Selected_Utilitie)

        self.Show()

        """The event catcher here."""
        wx.EVT_MENU_HIGHLIGHT_ALL(self, self.statusbar_status)

    def Create_New_Utilitie(self, event):
        pass

    def Delete_Selected_Utilitie(self, event):
        pass

    def statusbar_status(self, event):
        """Polemos: Change default statusbar field for showing menu help."""
        event_catcher = event.GetId()
        try: msg = self.menubar.GetHelpString(event_catcher)
        except: pass
        try: self.statusbar.SetStatusText(msg, 1)
        except: self.statusbar.SetStatusText('', 1) # Resets.

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

以下链接中的更多信息:

wxMenuItem Class Reference

Events Emitted by this Class

wx.MenuBar Methods Summary