简体   繁体   中英

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

In wxpython when we create a multi field statusbar, the first field is being used by default, to display the help texts of mouse hovered menu items.

for example in the code below:

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()

Whenever we hover over a menu item, the help text gets displayed automatically in the 1st field (by default).

An image illustrating my question while running the example above.

Please note that I am not asking how to SetStatusText the 2nd field with help texts but how to change the default field for displaying them.

Thanks.

OK, I finally found the answer.

to change the default field in the statusbar (where menu help strings are shown by default) you need to catch them with EVT_MENU_HIGHLIGHT_ALL(func) .

So by taking as an example the code in my question above, here are the changes:

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()

More info in the links below:

wxMenuItem Class Reference

Events Emitted by this Class

wx.MenuBar Methods Summary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM