I am experimenting with Tkinter and the thread mechanism. Can someone explain why this throws an exception:
<class '_tkinter.TclError'> out of stack space (infinite loop?)
and how can i solve this? Below is the code. By the way, I know that some people suggest using a stream module instead of a stream, but now I would like to use a stream module, which is easier to just imagine a mechanism.
from Tkinter import * import thread import time def main_thread(master): try: frame = Frame(master) frame.pack(side='bottom') scrollbar = Scrollbar(master) scrollbar.pack(side='right',fill='y') t = "Title" title = StringVar() title.set(t) ttl = Label(master, textvariable=title, font=("Helvetica", 18)) ttl.pack(side='top') cont = Text(master, font=("Helvetica",14), yscrollcommand=scrollbar.set) cont.pack(side='bottom') button = Button(frame,text="Exit", command=root.destroy) button.pack(side='bottom') n = 0 while 1: n += 1 cont.insert('end', str(n)+"\n") time.sleep(1) except Exception as e: print type(e), e if __name__ == '__main__': root = Tk() root.title("My counting application") thread.start_new_thread(main_thread, (root,))
Thanks, Luke
EDIT
I decided to replace
n = 0 while 1: n += 1 cont.insert('end', str(n)+"\n") time.sleep(1)
with
n = 0 def do_every_second(n): cont.insert("end", str(n) + "\n") n += 1 master.after(1000, do_every_second, n) do_every_second(n)
and challenge
main_thread(root)
instead
thread.start_new_thread(main_thread, (root,))
source share