tkinter回调异常_在python tkinter中处理异常(Handling exception in python tkinter)

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!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值