如何使用 wxPython 创建一个信息图标
How to create an info icon with wxPython
到目前为止,我未能使用 wxPython 创建通俗地称为 "info icon" 的东西。带有某种 'i' 图像的图标,在悬停时显示大工具提示。
我可以为图像添加 wx.StaticBitmap
,但它会忽略所有 SetToolTipString
或 SetToolTip(wx.ToolTip())
调用。或者我可以向 wx.StaticText
添加一个大工具提示,如下所示。
忽略图标还没有正确的大小。
不用说最终tooltip需要一个不同于面板背景色的背景色(这里不是重点)。我不能使用 wx.adv.RichToolTip
因为我在 wxPython 3.0.2.0 osx-cocoa.
有什么好的方法可以解决这个问题?
如果您创建一个 ID 为 wx.ID_HELP
的按钮,那么您将获得该平台的股票帮助按钮(如果有的话)。然后你可以像任何按钮一样用它做任何你想做的事。分配工具提示,在 EVT_BUTTON
事件中执行某些操作等。请参阅 StockButtons sample in the demo。如果库存图像或标签不能满足您的需求,那么您可以只使用 wx.BitmapButton 来显示您想要的图像,并且仍然具有标准的工具提示支持。
您可能还想了解的其他内容是 ContextHelp sample in the demo。它展示了如何使用 wx.ContextHelpButton
,当单击它时,应用程序将进入上下文帮助模式。然后将显示弹出提示 window,用于接下来单击的任何小部件。不完全符合您的要求,但可能很合适。
wxArtProvider 或许可以提供帮助http://docs.wxwidgets.org/trunk/classwx_art_provider.html
import wx
class Test(wx.Frame):
def __init__(self,parent,msg,title):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self, size=(300,400))
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
staticIcon = wx.BitmapButton(self.panel, bitmap=wx.ArtProvider.GetBitmap(wx.ART_WARNING), size=(32,32))
mainSizer.Add(staticIcon, flag=wx.ALL, border=10)
ttip = "xxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
staticIcon.SetToolTipString(ttip)
buttonText = wx.StaticText(self.panel, -1, msg, wx.DefaultPosition, wx.DefaultSize, 0)
mainSizer.Add(buttonText, flag=wx.ALL, border=10)
staticIcon.Bind(wx.EVT_BUTTON, self.OnButton)
self.SetSizer(mainSizer)
self.Show()
def OnButton(self, evt):
print "The button was pressed - display some help"
if __name__ == '__main__':
app = wx.App()
Test(None, "Dummy Exercise", "Test 123")
app.MainLoop()
如果您只想在鼠标悬停在图像上时显示工具提示,那么您需要将 wx.StaticBitmap
的实例绑定到 EVT_MOTION
:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING)
self.image = wx.StaticBitmap(self, bitmap=bmp)
self.image.Bind(wx.EVT_MOTION, self.on_mouse_over)
def on_mouse_over(self, event):
self.image.SetToolTipString('BLAH BLAH BLAH')
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Icon Mouser')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
当我 运行 这段代码时,我得到这样的结果:
到目前为止,我未能使用 wxPython 创建通俗地称为 "info icon" 的东西。带有某种 'i' 图像的图标,在悬停时显示大工具提示。
我可以为图像添加 wx.StaticBitmap
,但它会忽略所有 SetToolTipString
或 SetToolTip(wx.ToolTip())
调用。或者我可以向 wx.StaticText
添加一个大工具提示,如下所示。
不用说最终tooltip需要一个不同于面板背景色的背景色(这里不是重点)。我不能使用 wx.adv.RichToolTip
因为我在 wxPython 3.0.2.0 osx-cocoa.
有什么好的方法可以解决这个问题?
如果您创建一个 ID 为 wx.ID_HELP
的按钮,那么您将获得该平台的股票帮助按钮(如果有的话)。然后你可以像任何按钮一样用它做任何你想做的事。分配工具提示,在 EVT_BUTTON
事件中执行某些操作等。请参阅 StockButtons sample in the demo。如果库存图像或标签不能满足您的需求,那么您可以只使用 wx.BitmapButton 来显示您想要的图像,并且仍然具有标准的工具提示支持。
您可能还想了解的其他内容是 ContextHelp sample in the demo。它展示了如何使用 wx.ContextHelpButton
,当单击它时,应用程序将进入上下文帮助模式。然后将显示弹出提示 window,用于接下来单击的任何小部件。不完全符合您的要求,但可能很合适。
wxArtProvider 或许可以提供帮助http://docs.wxwidgets.org/trunk/classwx_art_provider.html
import wx
class Test(wx.Frame):
def __init__(self,parent,msg,title):
wx.Frame.__init__(self, None)
self.panel = wx.Panel(self, size=(300,400))
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
staticIcon = wx.BitmapButton(self.panel, bitmap=wx.ArtProvider.GetBitmap(wx.ART_WARNING), size=(32,32))
mainSizer.Add(staticIcon, flag=wx.ALL, border=10)
ttip = "xxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
ttip += "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
staticIcon.SetToolTipString(ttip)
buttonText = wx.StaticText(self.panel, -1, msg, wx.DefaultPosition, wx.DefaultSize, 0)
mainSizer.Add(buttonText, flag=wx.ALL, border=10)
staticIcon.Bind(wx.EVT_BUTTON, self.OnButton)
self.SetSizer(mainSizer)
self.Show()
def OnButton(self, evt):
print "The button was pressed - display some help"
if __name__ == '__main__':
app = wx.App()
Test(None, "Dummy Exercise", "Test 123")
app.MainLoop()
如果您只想在鼠标悬停在图像上时显示工具提示,那么您需要将 wx.StaticBitmap
的实例绑定到 EVT_MOTION
:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING)
self.image = wx.StaticBitmap(self, bitmap=bmp)
self.image.Bind(wx.EVT_MOTION, self.on_mouse_over)
def on_mouse_over(self, event):
self.image.SetToolTipString('BLAH BLAH BLAH')
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Icon Mouser')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
当我 运行 这段代码时,我得到这样的结果: