我使用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)
。我的猜测是我没有正确地使用这个函数,特别是因为错误消息是在重新加载之后呈现的。你知道吗
你知道吗?
提前谢谢!你知道吗
那里发生了一些奇怪的事情!
我能强迫它工作的唯一方法是每次重新定义
WebView
。我可能每次都对Destroy
过于热心了。这是可行的,但未必就是你所追求的。你知道吗
在wxPython中,总是有一个主线程负责绘制和更新GUI。在MWE中,
time.sleep(3)
行允许您等待3秒钟,然后再次尝试重新加载页面。但是,这样做的副作用是,在更新GUI并显示错误消息之前,先将程序的主线程发送到休眠状态。如果你移动时间。睡眠(3) 行到另一个线程GUI可以毫无问题地更新。这里有一个解决方案:相关问题 更多 >
编程相关推荐