37,720
社区成员
发帖
与我相关
我的任务
分享
#-*- coding: UTF-8 -*-
import Queue
import threading
import urllib2
import wx
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]
queue = Queue.Queue()
text_1 = []
text_2 = []
j = 0
for i in hosts:
text_1.append('self.txt1_%d'%j)
text_2.append('self.txt2_%d'%j)
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "编辑器")
self.panel = wx.ScrolledWindow(self, -1, style=wx.VSCROLL)
self.panel.SetScrollbars(1, 1, 1, 300)
sizer = wx.GridBagSizer()
j = 0
for i in hosts:
text_1[j] = wx.TextCtrl(self.panel, -1,"", size=(180,20))
text_2[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(180,90))
sizer.Add(text_1[j], pos=( 2*j, 0 ))
sizer.Add(text_2[j], pos=( 2*j + 1, 0 ))
j = j + 1
self.panel.SetSizer(sizer)
self.panel.Fit()
main()
def Message(self, text): # 写到这里我就不知道怎么办了:(
# 目的是将线程池产生的head, html在text_1[0], text_1[1], text_2[0], text_2[1]
# 中显示出来.当然如果您有更好的办法可以说说:)
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue, window):
threading.Thread.__init__(self)
self.queue = queue
self.window = window
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and prints first 1024 bytes of page
url = urllib2.urlopen(host)
head = url.read(1024)
html = url.read()
#signals to queue job is done
self.queue.task_done()
wx.CallAfter(self.window.Message, head, html) #通过CallAfter将head, html传入text1, text2
def main():
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
#wait on the queue until everything has been processed
queue.join()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
#-*- coding: UTF-8 -*-
import Queue
import threading
import urllib2
import wx
hosts = ["http://yahoo.com", "http://google.com",
"http://ibm.com", "http://apple.com"]
he = []
ht = []
n = 0
queue = Queue.Queue()
text_1 = []
text_2 = []
j = 0
for i in range(4):
text_1.append('self.txt1_%d'%j)
text_2.append('self.txt2_%d'%j)
class WorkerThread(threading.Thread):
def __init__(self, window):
threading.Thread.__init__(self)
self.window = window
def run(self):
#print '--------------'
wx.CallAfter(self.window.Message, he, ht)
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "编辑器")
self.panel = wx.ScrolledWindow(self, -1, style=wx.VSCROLL)
self.panel.SetScrollbars(1, 1, 1, 300)
sizer = wx.GridBagSizer()
j = 0
for i in hosts:
text_1[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(200,100))
text_2[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(200,180))
sizer.Add(text_1[j], pos=( 2*j , 0 ))
sizer.Add(text_2[j], pos=( 2*j , 1 ))
j = j + 1
#print j # j = 4
self.panel.SetSizer(sizer)
self.panel.Fit()
main()
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and prints first 1024 bytes of page
url = urllib2.urlopen(host)
head = url.read(1024)
html = url.read()
he.append(head)
ht.append(html)
if len(he) == number and len(ht)== number:
thread = WorkerThread(self)
thread.start()
#signals to queue job is done
self.queue.task_done()
def Message(self, he, ht):
for i in range(4):
#for i in number: #用这个循环要报错 number = len(hosts)
text_1[i].AppendText(he[i])
text_2[i].AppendText(ht[i])
def main():
#spawn a pool of threads, and pass them queue instance
for i in range(4):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
#wait on the queue until everything has been processed
queue.join()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()