【问题标题】:Issues with implementing wxpython's html2.WebViewHandler and html2.WebViewFSHandler实现 wxpython 的 html2.WebViewHandler 和 html2.WebViewFSHandler 的问题
【发布时间】:2015-10-25 04:59:03
【问题描述】:

我正在开发一个 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 内存文件

【问题讨论】:

  • 希望我能在这个问题上迅速给予赏金......我已经在这个问题上尝试了几天

标签: python wxpython


【解决方案1】:

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

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")

【讨论】:

  • 是的..我的错误是“about.js 而不是 about.htm”....但是我尝试使用上面的样本,但仍然得到一个 webview 窗口,显示“加载 URL 时出现问题内存:about.htm"
  • 没有别的了?没有发生什么错误的其他消息或解释?
  • 不..只是提到的带有消息的窗口..没有python错误或任何东西..这很奇怪
  • 大胆猜测,但请尝试使用LoadURL("memory://about.htm")。 memory: 看起来像一个协议,并且 URL 必须在协议后使用双斜杠进行格式化。另外,请确保您在任何地方(或在任何地方)都使用.htm,而不是.html。
  • hmmm.....同样的错误......甚至尝试将三个斜杠“///”像“memory:///about.htm”
【解决方案2】:

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

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

在此之后,您的 self.tester.LoadURL("memory:about.htm") 调用应该可以工作了。

wx.html2.WebViewFSHandler 只存在于 Phoenix,所以如果你不使用 Phoenix,那么恐怕你最好的选择可能是使用 WebView 的 SetPage 方法:

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

【讨论】:

  • 我正在使用 phoenix 3.0.3...我一直在使用“SetPage”,我试图避免使用它...我需要在需要时在内存中创建实际的 html 页面...在注册之前一个处理程序,我认为您需要在注册之前创建一个处理程序....此链接wxpython.org/Phoenix/docs/html/html2.WebView.html#html2-webview
  • 是的,您需要在注册之前创建一个处理程序,但是您已经在上面发布的代码中通过调用 wx.FileSystem.AddHandler(wx.MemoryFSHandler()) 来完成此操作。添加处理程序后您是否尝试过调用self.tester.RegisterHandler(wx.html2.WebViewFSHandler("memory"))?
  • 是的,但我的应用程序无法启动
  • 我想我看到了问题所在。我敢打赌,您在 Phoenix 上的原始示例代码错误,有关 AddFile 参数的错误不匹配。我将发布一个工作示例作为单独的答案。
  • 我将所有代码放在一个新文件中并用 python 运行它......我得到这个“AttributeError:'module'对象没有属性'WebViewFSHandler'”......似乎“WebView.New”方法没有任何名为 WebViewFSHandler 的方法....Pheonix 文档显示“WebViewFSHandler”只是“WebView”的方法....嗯....你能给我任何网络链接吗?跨度>
猜你喜欢
  • 2021-04-04
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多