2014-09-29 64 views
0

我有使用wx.StaticBitmap的应用程序中的图像,但我需要将图像添加到图像,当你点击图像时,它应该使用系统的默认浏览器打开该网址。如何放置这样的图像?wxPython图像与URL

回答

1

您需要将wx.StaticBitmap的实例绑定到wx.EVT_LEFT_DOWN,并使用Python的webbrowser模块打开url。您可以创建一个图像字典,其中每个图像映射到一个URL。然后,在加载图像时,将变量设置为该图像的路径,并将其用作字典中的键,以在单击图像时加载URL。

这里有一个非常简单的例子:

import webbrowser 
import wx 

######################################################################## 
class ImgPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     self.my_images = {"/path/to/image.jpg":"www.example.com"} 
     self.loaded = "/path/to/image.jpg" 

     img = wx.Image(self.loaded, wx.BITMAP_TYPE_ANY) 

     self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.BitmapFromImage(img)) 
     self.image_ctrl.Bind(wx.EVT_LEFT_DOWN, self.onClick) 

    #---------------------------------------------------------------------- 
    def onClick(self, event): 
     """""" 
     webbrowser.open(self.my_images[self.loaded]) 

######################################################################## 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="Images") 
     panel = ImgPanel(self) 
     self.Show() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    app.MainLoop()