Strange terminal behavior after quitting Tkinter application

Charles Sanders C.delete_this.Sanders at BoM.GOv.AU
Tue Apr 24 05:06:42 EDT 2007


Chris wrote:
> ... Quitting by typing 'sys.exit()' in the interpreter
> also works fine. Only quitting via the GUI seems to cause the
> problem.

	As previously stated, I know nothing about Tkinter,
but it definitely looks like there is some cleanup being skipped
on a GUI exit that is in fact being run on exit via ^D and
sys.exit from the terminal.

	If I remember correctly, the low level details of how
this sort of thing is usually handled on unix like systems is
for the application to
	- Do an ioctl TCGETA system call to get the current
	  settings, and save them somewhere.
	- Do an ioctl TCSETA system call to change the settings.
	- Arrange, via signal handlers, atexit, or other means
	  for a cleanup routine to be called on exit.
	- The cleanup routine does an ioctl TCSETA call with
	  the saved settings.
(all this would typically involve C calls)

I believe that in your case this last cleanup is being omitted
on exit via the GUI.

 > For the moment, including the line 'os.system("stty sane")'
 > before sys.exit() is the solution I'll use.

	If that is satisfactory, well and good. However, there
is a possibility that you may lose some settings that you would
prefer to keep. The terminal settings have been trashed, and
stty sane has restored a minimally workable set, but some
settings may not be what you expect.

	If you are on a unix like system (eg Linux), and can wrap
your Tkinter application in a shell script, you should be able to 
save/restore the settings outside of Tkinter and python, ie

#! /bin/sh
saved_settings=`stty -g` i    # Note backquotes, save current settings
python Tkinter-application.py # Or whatever
stty "$saved_settings"   i    # Restore settings

	Doing the save within the Tkinter application, with
(for example)  os.system("stty -g > saved_settings") and
os.system("stty `cat saved_settings`") may not work as
the setting may have been changed by Tkinter before you get
a chance to save them - I just do not know.

	Just in case, I did a google search. I am not familiar
with TKinter, but a couple of articles out there imply
that rather than calling sys.exit you should be calling a
TkInter routine root.destroy. I am not sure if root is a
variable for the main window (ie you call the destroy method
on the main window) or if it has some special Tkinter meaning. 
Presumably this routine cleans things up before calling sys.exit
or an equivalent.

Charles




More information about the Python-list mailing list