简体   繁体   中英

Why does Tkinter stack overflow on Windows?

This short Python script debugwin.py works great on my Linux machine:

>>> import debugwin
>>> l = []
>>> debuwin.watch(l)
0
>>> l.append(1)

However, people have told me that on Windows (Python 2.7.3 Windows 7) it sometimes doesn't update after you append, and sometimes gets a stack overflow:

>>> error in background error handler:
    out of stack space (infinite loop?) while executing "::tcl::Bgerror {out of stack
    space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack
    space (infinite loop?)Unable to format..."

How can the script overflow?

It can overflow if the code that was written to report a bug has a bug. When it tries to report a bug it calls the bug reporting code, but it has a bug so it tries to call the bug reporting code, ... I'm not saying that's the problem, but that's at least one way to get what you're seeing.

I'm not particularly surprised you get crashes with the debugwin.py code you linked to (at least, the version of that code at the time I write this). Tkinter isn't thread safe, and conventional wisdom is that it should only be run in the main thread of an application. It looks like the code creates a tk interpreter in a sub-thread. So, even if there's no bug in the bug reporting code, the fact that the tcl interpreter is running in a sub-thread makes me think it could be thread-related. Certainly, the fact that the crash seems random leads me to believe it's related to threading.

I see another thing that looks somewhat suspicious. All widgets have an update method which calls the standard tk update command. You have a widget that inherits from Label, and you created your own update method. That may not cause any problems, but it's a code smell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM