wxPython WebView:如何在错误时重新加载? [英] wxPython WebView: how to reload on error?

查看:198
本文介绍了wxPython WebView:如何在错误时重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wx.html2.WebView 可以在对话框中加载网站,该对话框可以正常运行.

I'm using wx.html2.WebView to load a Website in a Dialog, which is working fine.

问题:如果由于任何原因无法访问网站,则输出将为Could not connect: Connection refused.

Problem: If the Website can't be reached for any reason, the output will be Could not connect: Connection refused.

期望的行为:在每次失败的x秒后,尝试重新加载URL .

Desired behaviour: Try to reload the URL after x seconds on every fail.

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)中出现问题.我的猜测是我没有正确使用该功能,尤其是因为错误消息是在重新加载后呈现的.

The problem occurs in on_webview_error(self, evt). My guess is that I'm using the function not correctly, especially because the error message is rendered after the reloading.

有什么主意吗?
预先感谢!

Any idea?
Thanks in advance!

推荐答案

那里发生了一些奇怪的事情!
我强迫它工作的唯一方法是每次都重新定义WebView.我也可能每次都对Destroy充满热情.
这可行,但不一定完全符合您的要求.

There is something strange going on there!
The only way that I could force it to work was to redefine the WebView each time. I may be being over zealous with the Destroy each time as well.
This works but may not necessarily be exactly what you are after.

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 WebView:如何在错误时重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆