wxpython webview:如何在出错时重新加载?

2024-04-19 05:57:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用wx.html2.WebView在对话框中加载一个网站,这个对话框正常工作。你知道吗

问题:如果由于任何原因无法访问网站,输出将是Could not connect: Connection refused。你知道吗

所需行为:每次失败后,尝试在x秒后重新加载URL。你知道吗

import wx 
import wx.html2 
import time

URL = "http://mydomain.tld"

class MyBrowser(wx.Frame): 
  def __init__(self, *args, **kwds): 
    wx.Frame.__init__(self, *args, **kwds) 
    self.browser = wx.html2.WebView.New(self) 
    self.browser.Bind(wx.html2.EVT_WEBVIEW_ERROR, self.on_webview_error)

  def on_webview_error(self, evt):
    # Printing works
    print("Error: can't load page, try again in 3 seconds.")
    # Sleeping works
    time.sleep(3)
    # Reloading doesn't work
    self.browser.LoadURL(URL) # OR self.browser.Reload()
    # Weird: Error is rendered now


if __name__ == '__main__': 
  app = wx.App() 
  dialog = MyBrowser(None, -1) 
  dialog.browser.LoadURL(URL) 
  dialog.Show() 
  app.MainLoop() 

问题发生在on_webview_error(self, evt)。我的猜测是我没有正确地使用这个函数,特别是因为错误消息是在重新加载之后呈现的。你知道吗

你知道吗?
提前谢谢!你知道吗


Tags: importselfbrowserurltime网站onerror
2条回答

那里发生了一些奇怪的事情!
我能强迫它工作的唯一方法是每次重新定义WebView。我可能每次都对Destroy过于热心了。
这是可行的,但未必就是你所追求的。你知道吗

import wx
import wx.html2
import time

URL = "http://mydomain.tld"

class MyBrowser(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.url = URL
        self.browser = wx.html2.WebView.New(self, -1, size=(900,600))
        self.browser.Bind(wx.html2.EVT_WEBVIEW_ERROR, self.on_webview_error)
        self.browser.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.on_webview_load)
        self.retries = 0
        self.max_retries = 10

    def on_webview_error(self, evt):
        self.URL = evt.GetURL()
        print(self.URL)
        self.retries += 1
        if self.retries > self.max_retries: # Give up
            self.Destroy()
        print("Error {} of {} attempts to load {}, trying again in 3 seconds.".format(self.retries,self.max_retries,self.URL))
        if self.retries > 5: # Try alternate
            self.URL = "http://wxPython.org"
            print("Swapping to alternate Url "+self.URL)
        self.browser.Destroy()

        time.sleep(3)

        self.browser = wx.html2.WebView.New(self, -1, size=(900,600))
        self.browser.Bind(wx.html2.EVT_WEBVIEW_ERROR, self.on_webview_error)
        self.browser.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.on_webview_load)
        self.browser.LoadURL(self.URL)

    def on_webview_load(self, evt):
        print(self.URL, "Load complete")

if __name__ == '__main__':
  app = wx.App()
  dialog = MyBrowser(None, -1)
  dialog.browser.LoadURL(URL)
  dialog.Show()
  app.MainLoop()

在wxPython中,总是有一个主线程负责绘制和更新GUI。在MWE中,time.sleep(3)行允许您等待3秒钟,然后再次尝试重新加载页面。但是,这样做的副作用是,在更新GUI并显示错误消息之前,先将程序的主线程发送到休眠状态。如果你移动时间。睡眠(3) 行到另一个线程GUI可以毫无问题地更新。这里有一个解决方案:

import wx 
import wx.html2 
import time
import _thread
from pubsub import pub

URL = "https://mydomain.tld"

class MyBrowser(wx.Frame): 
    def __init__(self, *args, **kwds): 
        wx.Frame.__init__(self, *args, **kwds) 
        self.browser = wx.html2.WebView.New(self) 
        self.browser.Bind(wx.html2.EVT_WEBVIEW_ERROR, self.on_webview_error)
        self.counter = 0
        pub.subscribe(self.loadwww, 'Try Again')
        pub.subscribe(self.loadalt, 'Give UP')

    def loadwww(self):
        self.browser.LoadURL("https://mydomain.tld")

    def loadalt(self):
        self.browser.LoadURL("https://www.google.com")

    def on_webview_error(self, evt):
        self.counter += 1
        _thread.start_new_thread(self.wait, (3,))   

    def wait(self, sec):
        if self.counter <= 5:
            print(self.counter)
            print("Error: can't load page, try again in 3 seconds.")
            time.sleep(sec)
            wx.CallAfter(pub.sendMessage, 'Try Again')
        else:
            wx.CallAfter(pub.sendMessage, 'Give UP')    

if __name__ == '__main__': 
    app = wx.App() 
    dialog = MyBrowser(None, -1) 
    dialog.browser.LoadURL(URL) 
    dialog.Show() 
    app.MainLoop()

相关问题 更多 >