我正在开发一个 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 方案“内存:......”)以便 webview 可以加载 html 内存文件
最佳答案
你能发布你的完整代码吗?现在你正在尝试加载
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")
关于python - 实现 wxpython 的 html2.WebViewHandler 和 html2.WebViewFSHandler 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326354/