Wxpython状态栏中事件的描述

时间:2013-02-16 23:22:17

标签: python wxpython

我想知道如何在状态栏中添加按钮用途的描述。

我一直认为这是这样的 -

self.SetStatusText("text", btn1)

然而它似乎不起作用。

我让它用于显示窗口的名称,以及菜单栏操作的说明。但我无法让它为Button工作。

例如在菜单栏中 -

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

似乎按钮没有“附加”属性。

由于

1 个答案:

答案 0 :(得分:3)

提供用户帮助的最常用方法是使用工具提示:

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

如果您坚持使用StatusBar,则可以绑定EVT_ENTER_WINDOW和EVT_LEAVE_WINDOW。一个小例子:

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