Tkinter and thread. out of stack space (infinite loop?)

前端 未结 2 866
耶瑟儿~
耶瑟儿~ 2021-01-18 09:07

I\'m experimenting with Tkinter and the threads mechanism. Can anyone explain why this raises the exception:

 out of stack         


        
2条回答
  •  隐瞒了意图╮
    2021-01-18 09:33

    The error is correct - there's an infinite loop on one of the tkinter elements. The thread module has nothing to do with it. The specific line causing the error:

    cont.insert('end', str(n)+"\n")
    

    Since cont is a tkinter element, you can not run it in your infinite while loop. To do what you want, you would need to print to the console instead. This can be demonstrated if you replace the offending line with a simple:

    print(str(n)+"\n")
    

    Edit: If you truly want to achieve the same effect you originally intended, this post deals with a potential alternative.

    Edit2: I would assume the exception is a design choice by the tkinter library (although I'm no expert). This would make sense since tkinter already uses an infinite loop for its event loop. I would imagine an infinite loop would prevent tkinter from ever drawing the changes to the screen and instead the libraries authors chose to throw an exception instead. A print should work since there's nothing new to draw, plus it's in it's own thread allowing tkinter's event loop to continue.

提交回复
热议问题