I have written an application in Python Tkinter. I recently noticed that for one of the operation, it sometimes closes (without giving any error) if that operation failed. I have written a small program to illustrate the problem :-
import os
from Tkinter import *
def copydir():
src = "D:\\a\\x\\y"
dest = "D:\\a\\x\\z"
os.rename(src,dest)
master = Tk()
def callback():
global master
master.after(1, callback)
copydir()
print "click!"
b = Button(master, text="OK", command=copydir)
b.pack()
master.after(100, callback)
mainloop()
To reproduce the problem, open the folder which it will rename in “ms command prompt” such that renaming it will throw exception from Tkinter code.
My original code is using threading and is performing other tasks as well, so I have tried to make the operations in this test script as similar as possible.
Now, if I run this code by double clicking it, then program simply closes without throwing any error. But If I had been running this script from console, then exception messages are dumped on the console and atleast I got to know , something is wrong.
I can fix this code by using try/catch in the code where it tried to rename but I want to inform user about this failure as well. So I just want to know what coding approaches should be followed while writing Tkinter App's and I want to know:-
1) Can I make my script dump some stack trace in a file whenever user ran this by double clicking on it. By this atleast, I would know something is wrong and fix it.
2) Can I prevent the tkinter app to exit on such error and throw any exception in some TK dialog.
Thanks for help!!