概述
我一直试图使用多处理模块来更新Tkinter GUI,但是当我运行这段代码时,它会给出Pickling错误。# Test Code for Tkinter with threads
import Tkinter
from multiprocessing import Queue
import multiprocessing
import time
# Data Generator which will generate Data
def GenerateData():
global q
for i in range(10):
print "Generating Some Data, Iteration %s" %(i)
time.sleep(2)
q.put("Some Data from iteration %s n" %(i))
def QueueHandler():
global q, text_wid
while True:
if not q.empty():
str = q.get()
text_wid.insert("end", str)
# Main Tkinter Application
def GUI():
global text_wid
tk = Tkinter.Tk()
text_wid = Tkinter.Text(tk)
text_wid.pack()
tk.mainloop()
if __name__ == '__main__':
# Queue which will be used for storing Data
tk = Tkinter.Tk()
text_wid = Tkinter.Text(tk)
q = multiprocessing .Queue()
t1 = multiprocessing.Process(target=GenerateData,args=(q,))
t2 = multiprocessing.Process(target=QueueHandler,args=(q,text_wid))
t1.start()
t2.start()
text_wid.pack()
tk.mainloop()
错误:PicklingError: Can't pickle : it's not found as thread.lock
如果删除参数text_wid,则不会报告任何错误,但文本小部件不会使用来自queque的数据更新。
更新:
我修改了代码,以便在队列中有值时调用函数来更新GUI,从而防止Tkinter小部件被传递到单独的进程。现在,我没有得到任何错误,但小部件没有更新的数据。但是,如果我使用Threading和Multiprocessing模块的混合,即创建一个单独的线程来处理队列中的数据,那么它可以正常工作。我的问题是,当我在单独的进程中运行处理程序代码时,为什么不起作用。我没有正确地传递数据吗。下面是修改后的代码:# Test Code for Tkinter with threads
import Tkinter
import multiprocessing
from multiprocessing import Queue
import time
import threading
# Data Generator which will generate Data
def GenerateData(q):
for i in range(10):
print "Generating Some Data, Iteration %s" %(i)
time.sleep(2)
q.put("Some Data from iteration %s n" %(i))
def QueueHandler(q):
while True:
if not q.empty():
str = q.get()
update_gui(str)
#text_wid.insert("end", str)
# Main Tkinter Application
def GUI():
global text_wid
tk = Tkinter.Tk()
text_wid = Tkinter.Text(tk)
text_wid.pack()
tk.mainloop()
def update_gui(str):
global text_wid
text_wid.insert("end", str)
if __name__ == '__main__':
# Queue which will be used for storing Data
tk = Tkinter.Tk()
text_wid = Tkinter.Text(tk)
q = multiprocessing.Queue()
t1 = multiprocessing.Process(target=GenerateData,args=(q,))
t2 = multiprocessing.Process(target=QueueHandler,args=(q,))
t1.start()
t2.start()
text_wid.pack()
tk.mainloop()
最后
以上就是包容草丛为你收集整理的python用户界面模块_使用多处理模块更新Tkinter图形用户界面的全部内容,希望文章能够帮你解决python用户界面模块_使用多处理模块更新Tkinter图形用户界面所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复