简体   繁体   English

Wxpython状态栏中事件的描述

[英]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. 但我不能让它为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. 如果您坚持使用StatusBar,则可以绑定EVT_ENTER_WINDOW和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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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