简体   繁体   中英

Wxpython Description of event in status bar

I was wondering how to add the description of a button purpose in the status bar.

I've always thought it was something like this -

self.SetStatusText("text", btn1)

However it doesn't seem to work.

I have it working for displaying the name of the window, and also descriptions for the menu bar actions. But I can't get it working for the Button.

For example in the Menu bar -

Settings = goto.Append(wx.NewId(), "Settings", "Navigate to Settings Window")

it doesn't seem Buttons have an "Append" attribute.

Thanks

The most common way to provide user help would be to use tooltips:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()

        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, label="Test")
        self.button.SetToolTipString("This is for testing purposes!")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.button)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

If you insist on using StatusBar, you can bind EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW. A small example:

import wx

class TipButton(wx.Button):
    """Subclass of wx.Button, has the same functionality. 
    Allows user to set Status Bar help by using SetStatusText method.
    """
    current = None

    def __init__(self, *args, **kwargs):
        wx.Button.__init__(self, *args, **kwargs)

    def SetStatusText(self, help_string, status_bar_frame):
        self.help_string = help_string
        self.status_bar_frame = status_bar_frame
        self.Bind(wx.EVT_ENTER_WINDOW, self._ShowHelp)
        self.Bind(wx.EVT_LEAVE_WINDOW, self._HideHelp)       

    def _ShowHelp(self, e):
        TipButton.current = self.GetLabel()
        self.status_bar_frame.SetStatusText(self.help_string)

    def _HideHelp(self, e):
        if self.GetLabel() == TipButton.current:
            self.status_bar_frame.SetStatusText("")        

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()

        self.panel = wx.Panel(self)
      # Instead of 
      # self.button1 = wx.Button(self.panel, label="Test1", size=(150, 50))
        self.button1 = TipButton(self.panel, label="Test1", size=(150, 50))
        self.button1.SetStatusText("This is for testing purposes", self)
        self.button1.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u'Consolas'))

      # Instead of 
      # self.button2 = wx.Button(self.panel, label="Test2", size=(150, 50))
        self.button2 = TipButton(self.panel, label="Test2", size=(150, 50))
        self.button2.SetStatusText("This is jusst for fun!", self)
        self.button2.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u'Consolas'))

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.button1)
        self.sizer.Add(self.button2)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()      

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

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