python - 实现 wxpython 的 html2.WebViewHandler 和 html2.WebViewFSHandler 的问题

标签 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 方案“内存:......”)以便 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/

相关文章:

python - 使用 pyodbc 创建 Python 模块

macos - wxPython macOS 暗模式支持和 pyinstaller

python - 根据新客户编号重置累计金额

python - 操作系统错误: [Errno 0] Error in httplib2 request

macos - 如何在 conda 环境中在 Mac OS X 上运行 runnakerun?

python - wxpython中如何区分鼠标双击和单击

python - wxPython:BoxSizer 中的项目不会水平扩展,只会垂直扩展

python - 如何在PIL和numpy之间转换模式为 "1"的图像?

python - 在 julian date python 中创建一个日期范围

python - 根据指定深度动态回填 Pandas DataFrame