如何使用wxPython创建信息图标

问题描述:

我至今未能用wxPython创建俗称为“信息图标”的内容。带有某种“我”图像的图标,显示悬停时的大型工具提示。如何使用wxPython创建信息图标

我可以为图像添加wx.StaticBitmap,但它忽略所有SetToolTipStringSetToolTip(wx.ToolTip())调用。或者我可以将一个大的工具提示添加到wx.StaticText,如下所示。

enter image description here 忽略该图标尚未具有正确的尺寸。

不用说,最终工具提示需要与面板背景颜色不同的背景颜色(不是此处的重点)。我不能使用wx.adv.RichToolTip,因为我在wxPython 3.0.2.0 osx-cocoa上。

什么是解决此问题的好方法?

如果你想要做的是显示一个提示,当图像位于鼠标过来,那么你需要你的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() 

当我运行这段代码,我得到的是这样的:

enter image description here

+0

我认为问题是在osx-cocoa上'StaticBitmap'拒绝了'SetToolTipString'字符串。由于我不是该操作系统的用户,因此我无法对此进行评论。 –

+0

这就是我第一次想到的。当然,在我发布这个问题之前,我尝试了这一点。原来,如果你在'wx.EVT_MOTION'事件处理程序中设置它_does_的工作。 –

+0

您没有自定义工具提示,是吗?它是如何得到圆角和黑色背景的? wxPython依赖于那些系统设置吗? 巧合我发现了'wx.PopupTransientWindow'。我可以在鼠标悬停时显示一个看起来像一个丰富的工具提示(如演示中所示)的实例。当鼠标不在“触发图像”上时,找到正确的事件来杀死它可能会很棘手。 –

如果您创建一个ID为wx.ID_HELP的按钮,那么您将获得平台的股票帮助按钮(如果有)。然后你可以像任何按钮一样做任何你想做的事情。指定工具提示,在EVT_BUTTON事件中执行某些操作等。请参阅StockButtons sample in the demo。如果股票图片或标签不符合您的需求,那么您可以使用wx.BitmapButton来显示您想要的图片,并仍然有标准的工具提示支持。

你可能想要看的其他东西是ContextHelp sample in the demo。它显示了如何使用wx.ContextHelpButton,点击后将应用程序置于上下文帮助模式。随后将显示弹出式提示窗口,以显示接下来点击的任何小部件。不是你要求的,但它可能是一个很好的选择。

+0

谢谢,我添加了链接到示例,将研究它们。除此之外,与@ rolf-of-saxony相同的反馈......按钮的UX语义不太适合“悬停在图像上”。 –

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

enter image description here

+0

这看起来不错,谢谢。从用户体验角度来看,它应该是图像上的工具提示(即悬停),或者用户需要点击的按钮。 –