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

wxpythonwebview:如何在出错时重新加载?,python,python-3.x,webview,event-handling,wxpython,Python,Python 3.x,Webview,Event Handling,Wxpython,我使用在对话框中加载网站,工作正常 问题:如果由于任何原因无法访问网站,输出将是无法连接:连接被拒绝 所需行为:每次失败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, **kwd

我使用在对话框中加载网站,工作正常

问题:如果由于任何原因无法访问网站,输出将是
无法连接:连接被拒绝

所需行为:每次失败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() 
问题出现在Web视图上的
错误(self,evt)
。我的猜测是,我没有正确地使用该函数,特别是因为错误消息是在重新加载后呈现的

有什么想法吗?

提前谢谢

那里发生了一些奇怪的事情
我能强迫它工作的唯一方法是每次重新定义
WebView
。我可能每次都对
销毁
过于热心了。
这是可行的,但未必就是你想要的

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并显示错误消息之前,先将程序的主线程发送到睡眠状态。如果您将time.sleep(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()
导入wx
导入wx.html2
导入时间
导入线程
从pubsub导入pub
URL=”https://mydomain.tld"
类MyBrowser(wx.Frame):
定义初始值(self,*args,**kwds):
wx.Frame.\uuuu init\uuuux(self,*args,**kwds)
self.browser=wx.html2.WebView.New(self)
self.browser.Bind(wx.html2.EVT\u WEBVIEW\u错误,self.on\u WEBVIEW\u错误)
self.counter=0
订阅(self.loadwww,“重试”)
pub.subscribe(self.loadalt,“放弃”)
def loadwww(自我):
self.browser.LoadURL(“https://mydomain.tld")
def loadalt(自身):
self.browser.LoadURL(“https://www.google.com")
Web视图上的def错误(自身、evt):
self.counter+=1
_启动新线程(self.wait,(3,))
def等待(自身,秒):

如果self.counter我同意,这很奇怪,但至少它起作用了。非常感谢!:-)