Problems implementing wxpython html2.WebViewHandler and html2.WebViewFSHandler

I am working on a GUI program that some parts write in wxpython and some parts in css, html and javascript

Below are the codes taken from 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")

These codes show how:

  • add MemoryFSHandler and load the HTML string into the memory stream instead of placing html codes in the file and calling this file
  • Also this example is based on an html widget and not a web browser widget

Below are my codes (trial version and error)

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

I tried to find some examples on the Internet, but was unsuccessful

Question

-. html / (, URI ":....." ), webview html-

+4
2

?

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

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

EDIT:

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

Source: https://habr.com/ru/post/1613072/


All Articles