Tkinter error in exiting the app: Fatal Python Error 
Author Message
 Tkinter error in exiting the app: Fatal Python Error

import Tkinter
def quit(m, event=None): pass
Tkinter.Toplevel().bind('<Destroy>', quit)

Three lines above cause python 1.5.1 (#0) (running in Win NT 4.0)
to give the following error:

Fatal Python error: PyThreadState_Get: no current thread

abnormal program termination

Is this the correct behavior? How do I bind quits to windows?
IIRC, something was changed in window destroy mechanism
from Python version 1.4 to 1.5? Default binding of Destroy
was added?

--Jyrki



Sat, 30 Dec 2000 03:00:00 GMT  
 Tkinter error in exiting the app: Fatal Python Error

Quote:
> import Tkinter
> def quit(m, event=None): pass
> Tkinter.Toplevel().bind('<Destroy>', quit)

> Three lines above cause python 1.5.1 (#0) (running in Win NT 4.0)
> to give the following error:

> Fatal Python error: PyThreadState_Get: no current thread

> abnormal program termination

Hm...  I can reproduce this on Unix, too (from a script, not
interactively).  Will look into it.  However, as a quick workaround,
if I run the Tk main loop, like this:

import Tkinter
def quit(m, event=None): pass
w = Tkinter.Toplevel()
w.bind('<Destroy>', quit)
w.mainloop()

I don't get the error.  It may be a combination of uninitialized Tk
widgets and an unfortunate cleanup order when the program exits.

Why do you want to bind <Destroy>?

--Guido van Rossum (home page: http://www.python.org/~guido/)



Sat, 30 Dec 2000 03:00:00 GMT  
 Tkinter error in exiting the app: Fatal Python Error

Quote:
> It may be a combination of uninitialized Tk
> widgets and an unfortunate cleanup order when the program exits.

> Why do you want to bind <Destroy>?

<Destroy> binding was used to remove callbacks from variables to
widgets that are being destroyed. I have a much larger example
(about 300 kB of Python code) with mainloop that crashes similarly
even when I remove all the <Destroy> bindings. I was hoping that
this simple example would reveal something that would give a cure
to my problems for exiting an application. If you like, I can try to
reproduce this error without binding <Destroy>.

Here is a new version that crashes with mainloop:

----------------------------
import Tkinter

def quit(m=None, event=None): pass

def quitreal(m=None, event=None):
  import sys
  sys.exit()

a=Tkinter.Toplevel()
a.bind('<Destroy>', quitreal)
b=Tkinter.Button(a, text="Abnormal Termination", command=quitreal)
b.pack()
a.mainloop()
---------------------

and one more with only one toplevel window:

------------------
import Tkinter

def quitreal(m=None, event=None):
  import sys
  sys.exit()

b=Tkinter.Button(None, text="Abnormal Termination", command=quitreal)
b.bind('<Destroy>', quitreal)
b.pack()
b.mainloop()
---------------------------

--Jyrki



Sat, 30 Dec 2000 03:00:00 GMT  
 Tkinter error in exiting the app: Fatal Python Error
I think I've figured out the reason why this crashes.  When Python
exits, it cleans up after itself, and then calls any exit handlers
that have been registered by modules.  The _tkinter module registers
Tcl_Finalize as an exit
handler.  Now, Tcl_Finalize will destroy any windows that still exist,
and call your <Destroy> handler.  Unfortunately, at this point the
current thread state has already been cleaned up, and when the C code
that is about ready to call your Python code tries to touch the
current thread state, it dies.

As a temporary workaround, the easiest thing you can do is to get rid
of the call

        Py_AtExit(Tcl_Finalize);

in _tkinter.c -- it is there more to be politically correct than that
it currently does something useful (except when restarting the
interpreter; but I don't know if Tcl/Tk will survive that).

In the mean time, I'm going to see if perhaps I could do the
finalization order in Python differently, e.g. call the low-level exit
handlers before destroying the interpreter.  But I may decide just to
take it out; I've had enough trouble with it (see also the 1.5.1 patch
page).

--Guido van Rossum (home page: http://www.python.org/~guido/)



Sat, 30 Dec 2000 03:00:00 GMT  
 Tkinter error in exiting the app: Fatal Python Error
I have an alternative Python-only fix. I added the following function
call to the exit procedure.

def recursiveUnbindDestroy(w):
  w.unbind('<Destroy>')
  for c in w.children.values():
    recursiveUnbindDestroy(c)

recursiveUnbindDestroy(Tkinter._default_root)

Now it runs without crashing in the end. It really seems that
it is only the <Destroy> binding that causes the abnormal
termination and it is reasonably easy to remove those bindings
while the interpreter is still{*filter*} there :-)

However,  this may not be a working solution, if someone
had bound persistent operations to <Destroy>, for example,
saving state/configuration of a widget instance for next invokation.
Perhaps adding an artificial calling of the <Destroy> handlers before
the recursive unbinding would do the trick, but there must at least
a thousand ways to fumble in that.

--Jyrki



Sun, 31 Dec 2000 03:00:00 GMT  
 Tkinter error in exiting the app: Fatal Python Error

Quote:

> . . .

> Fatal Python error: PyThreadState_Get: no current thread

> abnormal program termination

By trial and error, I've found that in my complex (but
single-threaded) Tkinter applications, I need to

a) In the voluntary exit case
  a1) remove all '<Destroy>' bindings;
  a2) remove my exitfunc binding (del sys.__dict__['exitfunc'])
  a3) destroy all my toplevels by hand (for w in ...: w.destroy())
  a4) exit with sys.exit(0)

b) In the killed-from-outside case:
  b1) make a new Tk for any last-minute save-yourself user dialogs;

Hope this helps

ht
--
  Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
     2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440

                     URL: http://www.ltg.ed.ac.uk/~ht/



Sun, 31 Dec 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Fatal errors in 1.5 at exit time

2. Fatal Error on Exit

3. fatal error with 32 bits app in clarion

4. fatal error: internal error

5. error exiting mainloop in tkinter

6. error exiting my app

7. Fatal Python error: GC object already in linked list

8. Fatal Python error: GC object already in linked list

9. Fatal Python error: ceval: tstate mix-up

10. Fatal Python error: Interpreter not initialized (version mismatch?)

11. Fatal Python error: PyThreadState_Get

12. Fatal Python error: PyThreadState_Get: no current thread

 

 
Powered by phpBB® Forum Software