史蒂芬Sproat是在正確的軌道上(榮譽爲我把它 - 不能做它沒有你的建議) - 這裏是一個完整的解決方案中的相關內容:
import wx.html as html
import urllib2 as urllib2
import os
import tempfile
import threading
from Queue import Queue
class HTTPThread(threading.Thread):
def __init__(self, urlQueue, responseQueue):
threading.Thread.__init__(self)
self.urlQueue = urlQueue
self.responseQueue = responseQueue
def run(self):
# add error handling
url = self.urlQueue.get()
request = urllib2.Request(url)
response = urllib2.urlopen(request)
page = response.read()
self.responseQueue.put(page)
而且在你的派生類的HTML。HtmlWindow:
def OnOpeningURL(self, type, url):
if type == html.HTML_URL_IMAGE:
# If it is a tempfile already, just tell it to open it.
# Since it will be called again
# immediately after first failure only need to keep the last
# temp file within the object, and the former is closed!
if self.f is not None and self.f.name in url:
return html.HTML_OPEN
# if its not a tempfile, download asynchronously and redirect
urlq = Queue()
resq = Queue()
t = HTTPThread(urlq, resq)
t.start()
urlq.put_nowait(url)
while True:
if resq.empty():
# your task while waiting
time.sleep(0.1)
else:
img = resq.get()
break
self.f = tempfile.NamedTemporaryFile()
self.f.write(img)
self.f.seek(0)
return 'file://' + self.f.name
else:
return html.HTML_OPEN
我的應用程序這個工程很大,但如果你真正想擁有的圖像加載「像一個普通的網絡瀏覽器」,那麼你將需要比wx.html.HtmlWindow更多。但是,這是非阻塞的並且會正確加載圖像。
這並不能真正幫助他解決問題,因爲他不確定在哪裏綁定正在加載的圖像。 – 2010-09-29 15:07:24
我已經知道如何使用線程我只是不知道如何讓窗口在不同的線程中進行圖像加載。謝謝 – Blazer 2010-09-29 23:37:50
我在想你可以用線程下載圖像,然後使用PubSub或PostEvent來告訴你的應用程序顯示它。 – 2010-09-30 14:03:59