实现wxpython的html2.WebViewHandler和html2.WebViewFSHandler

时间:2015-10-25 04:59:04

标签: python wxpython

我正在开发一个GUI程序,其中一些部分用wxpython编写,一些部分用css,html和javascript编写

以下代码摘自http://wxpython.org/Phoenix/docs/html/MemoryFSHandler.html#memoryfshandler

def OnAbout(self, event):

    bcur = wx.BeginBusyCursor()



    wx.FileSystem.AddHandler(wx.MemoryFSHandler) #there is a bug here in this example wx.MemoryFSHandler should read wx.MemoryFSHandler()
    wx.MemoryFSHandler.AddFile("logo.pcx", wx.Bitmap("logo.pcx", wx.BITMAP_TYPE_PCX))
    wx.MemoryFSHandler.AddFile("about.htm",
                               "<html><body>About: "
                               "<img src=\"memory:logo.pcx\"></body></html>")

    dlg = wx.Dialog(self, -1, _("About"))

    topsizer = wx.BoxSizer(wx.VERTICAL)

    html = wx.html.HtmlWindow(dlg, size=wx.Size(380, 160), style=wx.HW_SCROLLBAR_NEVER)
    html.SetBorders(0)
    html.LoadPage("memory:about.htm")
    html.SetSize(html.GetInternalRepresentation().GetWidth(),
                 html.GetInternalRepresentation().GetHeight())

    topsizer.Add(html, 1, wx.ALL, 10)
    topsizer.Add(wx.StaticLine(dlg, -1), 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
    topsizer.Add(wx.Button(dlg, wx.ID_OK, "Ok"),
                 0, wx.ALL | wx.ALIGN_RIGHT, 15)

    dlg.SetAutoLayout(True)
    dlg.SetSizer(topsizer)
    topsizer.Fit(dlg)
    dlg.Centre()
    dlg.ShowModal()

    wx.MemoryFSHandler.RemoveFile("logo.pcx")
    wx.MemoryFSHandler.RemoveFile("about.htm")

这些代码说明了如何:

  • 添加MemoryFSHandler并将HTML字符串加载到内存流中,而不是将html代码放在文件中并调用该文件
  • 此示例也基于 html小部件非webview小部件

以下是我的代码(试错)

class About(wx.Frame):
    def __init__(self):
        wx.Panel.__init__(self,None,-1,title="This is a working example",size=(700,700))
class Test(wx.Frame):
    """Contact author: contribute a word or send a occurences of bugs"""
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self,None,-1,title,pos,size)
        self.tester=wx.html2.WebView.New(self)
        #self.tester.RegisterHandler(wx.html2.WebViewHandler())
        wx.FileSystem.AddHandler(wx.MemoryFSHandler())
        #self.tester.SetPage("""
        wx.MemoryFSHandler().AddFile("about.js","""
document.write("IT is working")
""")
        self.tester.LoadURL("memory:about.htm")

我尝试在网上搜索一些例子,但不幸的是

问题

如何为 webview小部件创建处理程序。此处理程序应在内存流/文件中加载任何html字符串(例如使用URI方案“memory:.....”),以便 webview 可以加载html内存文件

2 个答案:

答案 0 :(得分:3)

你能发布完整的代码吗?现在你正在尝试加载

self.tester.LoadURL("memory:about.htm")

但您注册的唯一内存文件是about.js。如果您想引用about.htm,您必须先注册:

wx.FileSystem.AddHandler(wx.MemoryFSHandler())
wx.MemoryFSHandler().AddFile("about.js", 'document.write("IT is working")')
wx.MemoryFSHandler().AddFile("about.htm",
                             """<html>
                                    <script src="memory:about.js"></script>
                                    <body><h2>It lives!</h2></body>
                                </html>""")
self.tester.LoadURL("memory:about.htm")

答案 1 :(得分:3)

您需要的是wx.html2.WebViewFSHandler。我自己没有尝试过,所以我基于wxWidgets WebView示例,但是在创建wx.MemoryFSHandler以使用WebView注册内存处理程序后,您应该能够执行以下操作:

self.tester.RegisterHandler(wx.html2.WebViewFSHandler("memory"))

在此之后,你的self.tester.LoadURL(&#34; memory:about.htm&#34;)调用应该有效。

wx.html2.WebViewFSHandler只存在于Phoenix,所以如果您不使用Phoenix,那么我担心您最好的选择可能是使用WebView的SetPage方法:< / p>

html_data = """<html>
           <script>document.write("IT is working");</script>
           <body><h2>It lives!</h2></body>
           </html>"""
self.tester.SetPage(html_data, "")

编辑:

我为Phoenix添加了一个完整的工作示例,以展示如何使其工作。

import wx
import wx.html2

class About(wx.Frame):
    def __init__(self):
        wx.Panel.__init__(self,None,-1,title="This is a working example",size=(700,700))

class Test(wx.Frame):
    """Contact author: contribute a word or send a occurences of bugs"""
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self,None,-1,title,pos,size)
        self.tester=wx.html2.WebView.New(self)
        memoryfs = wx.MemoryFSHandler()
        wx.FileSystem.AddHandler(memoryfs)
        wx.MemoryFSHandler.AddFileWithMimeType("about.js", u'document.write("IT is working")', 'text/plain')
        wx.MemoryFSHandler.AddFileWithMimeType("about.htm",
                             u"""<html>
                                    <script src="memory:about.js"></script>
                                    <body><h2>It lives!</h2></body>
                                </html>""", 'text/html')
        self.tester.RegisterHandler(wx.html2.WebViewFSHandler("memory"))
        self.tester.LoadURL("memory:about.htm")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = Test("Hello", (20, 20), (800, 600))
    frame.Show()
    app.MainLoop()