python tkinter with threading causing crash -


i have written python tkinter code using threads tkinter wizard updates automatically tkinter mainloop running in main thread , background process running in separate thread. noticed, python crashes after time when running code. random in nature python crashes of time. have written small test code can show problem (my original code similar having real processes , many other features, sharing test code).

######################################################################  # test code tkinter threads import tkinter import threading import queue import time  # data generator generate data def generatedata(q):     in range(1000000):         #print "generating data, iteration %s" %(i)         time.sleep(0.01)         q.put("some data iteration %s. putting data in queue testing" %(i))  # queue used storing data q = queue.queue()  def queuehandler(widinst, q):     linecount = 0     while true:         print "running"         if not q.empty():             str = q.get()             linecount = linecount + 1             widinst.configure(state="normal")             str = str + "\n"             widinst.insert("end", str)             if linecount > 100:                 widinst.delete('1.0', '2.0')                 linecount = linecount - 1             widinst.see('end')             widinst.configure(state="disabled")  # create thread , run gui & queuehadnler in tk = tkinter.tk() scrollbar = tkinter.scrollbar(tk) scrollbar.pack(side='right', fill='y' ) text_wid = tkinter.text(tk,yscrollcommand=scrollbar.set) text_wid.pack() t1 = threading.thread(target=generatedata, args=(q,)) t2 = threading.thread(target=queuehandler, args=(text_wid,q)) t2.start() t1.start()  tk.mainloop() ###################################################################### 

to reproduce:

if open code in idle , run it, appeared in hang state. reproduce, modify sleep time 0.1 0.01 , run it. after stop application, , modify 0.01, save , run it. time run , after time, python stop working. using windows 7 (64 bit).

question

i have submitted python bugs , got rejected. got idea 1 of stackoverflow question use queue writing in tkinter. can 1 please suggest should done handle it.

edited code:

# test code tkinter threads import tkinter import threading import queue import time  # data generator generate data def generatedata(q):     in range(1000000):         #print "generating data, iteration %s" %(i)         time.sleep(0)         q.put("some data iteration %s. putting data in queue testing" %(i))  # queue used storing data q = queue.queue()  def queuehandler():     global widinst, q     linecount = 0     if not q.empty():         str = q.get()         linecount = linecount + 1         widinst.configure(state="normal")         str = str + "\n"         widinst.insert("end", str)         if linecount > 100:             widinst.delete('1.0', '2.0')             linecount = linecount - 1         widinst.see('end')         widinst.configure(state="disabled")         tk.after(1,queuehandler)  # create thread , run gui & queuehadnler in tk = tkinter.tk() scrollbar = tkinter.scrollbar(tk) scrollbar.pack(side='right', fill='y' ) text_wid = tkinter.text(tk,yscrollcommand=scrollbar.set) text_wid.pack() t1 = threading.thread(target=generatedata, args=(q,)) #t2 = threading.thread(target=queuehandler, args=(text_wid,q)) #t2.start() widinst = text_wid t1.start() tk.after(1,queuehandler) tk.mainloop() 

tkinter not thread safe; cannot access tkinter widgets anywhere main thread. need refactor code queuehandler runs in main thread.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -