使用多处理模块更新Tkinter GUI [英] Using Multiprocessing module for updating Tkinter GUI

查看:82
本文介绍了使用多处理模块更新Tkinter GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使用Multiprocessing模块来更新Tkinter GUI,但是当我运行此代码时,它给出了酸洗错误.

I have been trying to use Multiprocessing module for updating Tkinter GUI but when I run this code, it is giving Pickling error.

# 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 <type 'thread.lock'>: it's not found as thread.lock

如果删除参数text_wid,则不会报告任何错误,但不会使用来自queque的数据更新文本小部件.

If I remove the argument text_wid, then no error is reported but text widget is not updated with the data from the queque.

更新:

我修改了代码,以便在队列中有值时调用该函数以更新GUI,从而防止将Tkinter小部件传递给单独的进程.现在,我没有收到任何错误,但未使用数据更新小部件.但是,如果我混合使用ThreadingMultiprocessing模块,即创建一个单独的线程来处理队列中的数据,那么它可以正常工作.我的问题为什么当我在单独的过程中运行处理程序代码时,它为什么不起作用.我没有正确传递数据吗?下面是修改后的代码:

I modified code so as to call the function to update the GUI whenever there is value in queue, thus preventing Tkinter widgets from being passed to separate process. Now, I am not getting any error but the widget is not updated with the data. However if i use mix of Threading and Multiprocessing module i.e. create a separate thread for handling data from the queue, then it works fine. My question why didn't it worked when i run the handler code in separate process. Am I not passing the data correctly. Below is the modified code:

# 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()

推荐答案

# Test Code for Tkinter with threads
import Tkinter as Tk
import multiprocessing
from Queue import Empty, Full
import time

class GuiApp(object):
   def __init__(self,q):
      self.root = Tk.Tk()
      self.root.geometry('300x100')
      self.text_wid = Tk.Text(self.root,height=100,width=100)
      self.text_wid.pack(expand=1,fill=Tk.BOTH)
      self.root.after(100,self.CheckQueuePoll,q)

   def CheckQueuePoll(self,c_queue):
      try:
         str = c_queue.get(0)
         self.text_wid.insert('end',str)
      except Empty:
         pass
      finally:
         self.root.after(100, self.CheckQueuePoll, c_queue)

# 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))


if __name__ == '__main__':
# Queue which will be used for storing Data

   q = multiprocessing.Queue()
   q.cancel_join_thread() # or else thread that puts data will not term
   gui = GuiApp(q)
   t1 = multiprocessing.Process(target=GenerateData,args=(q,))
   t1.start()
   gui.root.mainloop()

   t1.join()
   t2.join()

这篇关于使用多处理模块更新Tkinter GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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