Using Multiprocessing module for updating Tkinter GUI
You missed out an important part, you should protect your calls with a __main__
trap:
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,)) ....
Note that the Queue is passed as a parameter rather than using a global.
Edit: just spotted another issue, you should be using Queue
from the multiprocessing
module, not from Queue
:
from multiprocessing import Queue
# Test Code for Tkinter with threadsimport Tkinter as Tkimport multiprocessingfrom Queue import Empty, Fullimport timeclass 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 Datadef 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()