使用多重模块更新Tkinter的GUI(Using Multiprocessing module f

2019-08-02 07:50发布

我一直在试图用多处理模块,用于更新Tkinter的GUI,但是当我运行这段代码,它给酸洗错误。

# 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数据。

更新:

我修改代码以便调用来更新每当有在队列值的GUI的功能,从而防止Tkinter的窗口小部件被传递到单独的过程。 现在,我没有得到任何错误,但小部件不更新的数据。 但是如果我使用的混合ThreadingMultiprocessing模块即创建一个单独的线程从队列中处理数据,然后它工作正常。 我的问题为什么没有它的工作时,我在运行单独的进程中处理程序代码。 我不是正确地传递数据。 下面是修改后的代码:

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

Answer 1:

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


Answer 2:

你错过了一个重要的组成部分,应保护与您的通话__main__陷阱:

if __name__ == '__main__': 
    q = Queue.Queue()
    # Create a thread and run GUI & QueueHadnler in it
    t1 = multiprocessing.Process(target=GenerateData,args=(q,))
    t2 = multiprocessing.Process(target=QueueHandler,args=(q,))

    ....

需要注意的是队列作为参数传递,而不是使用一个全球性的。

编辑:刚刚发现了另外一个问题,你应该使用Queuemultiprocessing模块,而不是从Queue

from multiprocessing import Queue


文章来源: Using Multiprocessing module for updating Tkinter GUI