2015-10-25 38 views
4

我對其中的一些部件都寫在wxPython和CSS,HTML一些地方和javascript問題與實施的wxPython的html2.WebViewHandler和html2.WebViewFSHandler

下面的代碼的GUI程序的工作是從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代碼的S IN文件並調用該文件
  • 另外這個例子是基於HTML控件不是web視圖控件

下面是我的代碼(試錯)

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內存文件

+0

希望我能很快把賞金這個問題...我一直在嘗試了幾天就這個問題 – repzero

回答

3

你能發佈你的完整碼?現在您正在嘗試加載

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

Yes..my誤差「about.js代替about.htm中」 .. ..然而,我嘗試使用上面的標本,但仍然得到一個web瀏覽窗口,指出「加載URL內存時發生的問題:about.htm」 – repzero

+0

而沒有別的?沒有其他的消息或*發生了什麼*錯誤的解釋? – BoppreH

+0

沒有..正是所提到的窗口的消息....沒有python錯誤或任何..這很奇怪 – repzero

3

你需要的是wx.html2.WebViewFSHandler。我還沒有嘗試過這個自己,所以我立足這一關wxWidgets的WebView中的例子,但你應該能夠做到創建wx.MemoryFSHandler註冊到的WebView內存處理程序後執行以下操作:

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

之後,你的self.tester.LoadURL(「內存:about.htm」)調用應該工作。

wx.html2.WebViewFSHandler只存在於鳳凰城,雖然如此,如果你不使用鳳凰,那麼恐怕你最好的選擇可能是使用的WebView SetPage方法來代替:

html_data = """<html> 
      <script>document.write("IT is working");</script> 
      <body><h2>It lives!</h2></body> 
      </html>""" 
self.tester.SetPage(html_data, "") 

編輯:

我爲鳳凰添加一個完整的工作示例,以顯示如何使其工作。

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

我使用鳳凰3.0.3 ...我一直在使用「SetPage」,我試圖避免......我需要在需要時在內存中創建實際的HTML頁面..在註冊處理程序之前,我認爲您需要在註冊它之前創建一個處理程序.... this鏈接http://wxpython.org/Phoenix/docs/html/html2.WebView.html#html2-webview – repzero

+0

是的,你需要在註冊之前創建一個處理程序,但是你已經通過調用'wx.FileSystem來做到這一點。 AddHandler(wx.MemoryFSHandler())'在你上面發佈的代碼中。您是否在添加處理程序後嘗試調用'self.tester.RegisterHandler(wx.html2.WebViewFSHandler(「memory」))'? –

+0

是的,我做了,但我的應用程序無法啓動 – repzero