如何在wxPython框架中单击时使用webbrowser打开链接

时间:2015-04-08 07:48:27

标签: html webview wxpython

我正在使用wxPython编写一个小应用程序。我已经将数据保存为HTML字符串。我使用wx.html2.webview来显示我的HTMl。 这是我的代码简化:

class MyBrowser(wx.Dialog):
def __init__(self, *args, **kwds):
    wx.Dialog.__init__(self, *args, **kwds)
    sizer = wx.BoxSizer(wx.VERTICAL)
    self.browser = wx.html2.WebView.New(self)
    sizer.Add(self.browser, 1, wx.EXPAND, 10)
    self.SetSizer(sizer)
    self.SetSize((800, 700))
if __name__ == '__main__':

  app = wx.App()
  dialog = MyBrowser(None, -1)
  dialog.browser.SetPage(HTMLstring,"")
  dialog.Show()
  app.MainLoop()

我的HTML字符串中有图片和链接。我已成功显示我的HTML字符串。但我的问题是:我希望默认的webbrowser可以在我点击它时打开链接。

那我应该怎么做。有人可以提供这样一个例子的链接或(更好的是),在这里发一个简短的片段,说明如何做到这一点?

顺便说一句,在我发现它无法以正确的方式显示我的HTML字符串之前,我使用了wx.HTML窗口。

这是我的HTMLstring被简化:

    <html>
     <head>
        <style type="text/css">
        div#container{background-color:#F0F8FF;width:700px}
        div#content {height:200px;width:400px;float:left;word-break: break-all;}
        div#predict{width:700px;}
        .box {height:200px;width:300px;overflow:hidden;background-repeat: no-repeat;background-position:center center;float:left;}
         img{margin-top: -50px;}
         a:link{ text-decoration:none;}
        </style>
     </head>
     <body>
      <br>
      <hr />
      <p>SCCA Predicted:</p>
       <div>
        <a href="http://sideeffects.embl.de/se/C0423006"target="_blank">Eye discharge 1.0,  </a><a href="http://sideeffects.embl.de/se/C0423602"target="_blank">Sensation of foreign body 0.99901248481,    </a>
      </div>
      </body></html>

2 个答案:

答案 0 :(得分:1)

没有一个好方法可以做到这一点。您可以使用webview的EVT_WEBVIEW_NAVIGATING事件。但是,正如文档所述,如果网页上有多个框架,则此事件将被多次触发。如果你想走那条路并尝试一下,那么我建议使用Python的webbrowser模块来打开你的默认浏览器。

这是一个快速演示:

import webbrowser
import wx 
import wx.html2 

class MyBrowser(wx.Dialog): 

    def __init__(self, *args, **kwds): 
        wx.Dialog.__init__(self, *args, **kwds) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        self.browser = wx.html2.WebView.New(self) 
        self.browser.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.onNav)
        self.browser.Bind(wx.html2.EVT_WEBVIEW_NEWWINDOW, self.onNewWindow)
        sizer.Add(self.browser, 1, wx.EXPAND, 10) 
        self.SetSizer(sizer) 
        self.SetSize((700, 700)) 

    def onNav(self, event):
        print "onNav called"
        url = event.GetURL()
        print url
        webbrowser.open(url)

    def onNewWindow(self, event):
        print "onNewWindow called"
        url = event.GetURL()
        print url
        webbrowser.open(url)


if __name__ == '__main__': 
    html = """<html>
         <head>
            <style type="text/css">
            div#container{background-color:#F0F8FF;width:700px}
            div#content {height:200px;width:400px;float:left;word-break: break-all;}
            div#predict{width:700px;}
            .box {height:200px;width:300px;overflow:hidden;background-repeat: no-repeat;background-position:center center;float:left;}
             img{margin-top: -50px;}
             a:link{ text-decoration:none;}
            </style>
         </head>
         <body>
          <br>
          <hr />
          <p>SCCA Predicted:</p>
           <div>
            <a href="http://sideeffects.embl.de/se/C0423006" >Eye discharge 1.0,  </a><a href="http://sideeffects.embl.de/se/C0423602" >Sensation of foreign body 0.99901248481,    </a>
          </div>
          </body></html>
          """
    app = wx.App() 
    dialog = MyBrowser(None, -1) 
    dialog.browser.SetPage(html, '') 
    dialog.Show() 
    app.MainLoop() 

答案 1 :(得分:0)

我使用桌面模块,使用Mike的示例编辑,但使用桌面模块而不是webbrowser。

import wx 
import wx.html2

import desktop

html = """
<html>
<head>
   <style type="text/css">
   div#container{background-color:#F0F8FF;width:700px}
   div#content {height:200px;width:400px;float:left;word-break: break-all;}
   div#predict{width:700px;}
   .box {height:200px;width:300px;overflow:hidden;background-repeat: no-repeat;background-position:center center;float:left;}
    img{margin-top: -50px;}
    a:link{ text-decoration:none;}
   </style>
</head>
<body>
 <br>
 <hr />
 <p>SCCA Predicted:</p>
  <div>
   <a href="http://sideeffects.embl.de/se/C0423006" >Eye discharge 1.0,  </a><a href="http://sideeffects.embl.de/se/C0423602" >Sensation of foreign body 0.99901248481,    </a>
 </div>
 </body></html>
"""

class MyBrowser(wx.Dialog): 

    def __init__(self, *args, **kwds): 
        wx.Dialog.__init__(self, *args, **kwds) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        self.browser = wx.html2.WebView.New(self) 
        self.browser.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.onNav)
        sizer.Add(self.browser, 1, wx.EXPAND, 10) 
        self.SetSizer(sizer) 
        self.SetSize((700, 700)) 

    def onNav(self, event):
        print "onNav called"
        url = event.GetURL()
        print url
        self.openExternal(url)

    def openExternal(self, url, doclear=False):
        """
        Open an external file with desktop module.

        :param `url`: the file url to open in external program
        :param `doclear`: clear InfoBar message

        """
        wx.BeginBusyCursor()
        try:
            desktop.open(url.strip())
        except OSError:
            text = (_(u'Your default browser could not be opened, \
    or the external program was not found or no \
    external program is defined for this file.'))
        wx.EndBusyCursor()

if __name__ == '__main__': 
    app = wx.App()
    with MyBrowser(None, -1) as dlg:
        dlg.browser.SetPage(html, '') 
        dlg.ShowModal() 
    app.MainLoop()

我也使用&#39;&#39;要创建对话框,这可以确保在完成对话时将其销毁。

https://pypi.python.org/pypi/desktop

如果您在Py3上需要它,我做了一些更改,您可以在这里: https://bitbucket.org/wbruhin/desktop

相关问题