Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

python - wxPython WebView example

I am writing a small reporting app using wxPython (wxAUI). I want to render my data as HTML, to be displayed in a WebView 'widget'. I am looking for a sample 'hello world' snippet that will show how to display/render an HTML string in a WebView widget - but have been unable to find a single example - and the WebView widget does not seem to be well documented.

Could someone please provide a link to such an example or (better still), post a short snippet here that shows how to use the WebView widget to render an HTML string?

# sample html string to display in WebView widget
html_string = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
       <title>Hello World!</title>
       <script type="text/javascript" src="jquery.js"></script>
       <style type="text/css" src="main.css"></style>
    </head>
    <body>
        <span id="foo">The quick brown fox jumped over the lazy dog</span>
        <script type="text/javascript">
        $(document.ready(function(){
           $("span#foo").click(function(){ alert('I was clicked!'); });
         });
        </script>
    </body>
</html>
"""
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a simple example that works for me.

Make sure you are running the latest version of wxpython. (wxpython 2.9)

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) 
    sizer.Add(self.browser, 1, wx.EXPAND, 10) 
    self.SetSizer(sizer) 
    self.SetSize((700, 700)) 

if __name__ == '__main__': 
  app = wx.App() 
  dialog = MyBrowser(None, -1) 
  dialog.browser.LoadURL("http://www.google.com") 
  dialog.Show() 
  app.MainLoop() 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...