I have a very strange unexpected problem with Python 2.7.2 under Windows 7..
This code doesn't quit:
import gtk
import win32ui
w = gtk.Window()
w.connect("destroy", gtk.main_quit)
w.show_all()
gtk.main()
print 'stop-point'
quit()
The window closes, I get 'stop point', and all should be ok.
But console doesn't close.
Even quit()
doesn't help.
The problem is in import win32ui
If I remove it, everything works fine.
Also, in version with just
import gtk
import win32ui
everything works.
What is the problem with win32ui? How do I force my app to close?
It happens even without GTK:
import win32gui, win32ui
try:
result = win32gui.GetOpenFileNameW()
except win32gui.error as result:
pass
print result
# script ends but python doesn't exit
The problem is caused by a bug in pywin32: https://sourceforge.net/tracker/?func=detail&aid=3562998&group_id=78018&atid=551954
I came up with a horrible hack that serves as a workaround until the bug in pywin32 is fixed. Put this code at the end of your exit routine. Clean up as much as you can before you execute this. The workaround uses the Windows taskkill
tool to terminate the current task.
import os
# kill this process with taskkill
current_pid = os.getpid()
os.system("taskkill /pid %s /f" % current_pid)
Registering the above code as a function with atexit
might allow Python to do some cleanup first:
import atexit, os
def taskkill_this():
# kill this process
current_pid = os.getpid()
os.system("taskkill /pid %s /f" % current_pid)
atexit.register(taskkill_this)