实现wxpython的html2.WebViewHandler和html2.WebViewFSHand的问题

2024-05-17 20:02:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个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")

这些代码显示了如何:

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

以下是我的代码(试错)

^{pr2}$

我试着在网上搜索一些例子,但很不幸

问题

如何为webview小部件创建处理程序。这个处理程序应该加载内存流/文件中的任何html字符串(例如使用URI方案)记忆:。。。。。)以便webview可以加载html内存文件


Tags: 文件内存代码add部件htmllogoabout
2条回答

你需要的是wx.html2.WebViewFSHandler。我自己还没有尝试过,所以我基于wxWidgets WebView示例,但是在创建wx.MemoryShandler公司要向WebView注册内存处理程序:

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

在这之后,你的self.tester.LoadURL("内存:about.htm)调用应该有效。在

在wx.html2.WebViewFSHandler只存在于Phoenix中,因此,如果您不使用Phoenix,那么您最好的选择可能是使用WebView的SetPage方法:

^{pr2}$

编辑:

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

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

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

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

^{pr2}$

相关问题 更多 >