我应该在tkinter中做出沉默的例外吗? [英] Should I make silent exceptions louder in tkinter?
问题描述
如果我从终端运行以下代码,我会在终端中收到一个有用的错误消息:
import Tkinter as tk
master = tk.Tk()
def callback():
raise UserWarning(Exception!)
b = tk.Button(master,text =这会引发一个例外,command = callback)
b.pack()
tk.mainloop()
但是,如果我没有运行终端(比如双击一个图标),那么错误消息就被抑制了。 >
在我真正的,更复杂的Tkinter应用程序中,我喜欢GUI有一点防撞。我不喜欢我的用户很难给我有用的反馈来解决所产生的意外行为。
我该如何处理?有没有一种标准的方式来展示回溯或stderror或什么在Tkinter应用程序?我正在寻找比放置尝试/除了所有地方更优雅的东西。
编辑:Jochen Ritzel在下面给出了一个很好的答案,弹出一个警告框,并提到附加它上课只要这样做:
导入Tkinter为tk
import traceback,tkMessageBox
类App:
def __init __(self,master):
master.report_callback_exception = self.report_callback_exception
self.frame = tk.Frame(master)
self.frame.pack )
b = tk.Button(
self.frame,text =这将导致异常,
command = self.cause_exception)
b.pack()
def cause_exception(self):
a = []
aa = 0 #A追溯使得这个容易捕获和修复
def report_callback_exception(self,* args )
err = traceback.format_exception(* args)
tkMessageBox.showerror('Exception',err)
root = tk.Tk()
app = App(root)
root.mainloop()
我的剩下的混乱:Jochen提到可能性具有不同的异常报告功能没有框架我还没有看到如何做到这一点。这是很明显的吗?
有 report_callback_exception
来做到这一点: / p>
import traceback
import tkMessageBox
#你通常会把它放在App类
def show_error(self,* args):
err = traceback.format_exception(* args)
tkMessageBox.showerror('Exception',err)
#但这样也可以
tk.Tk.report_callback_exception = show_error
如果没有导入Tkinter as tk然后执行
Tkinter.Tk.report_callback_exception = show_error
If I run the following code from a terminal, I get a helpful error message in the terminal:
import Tkinter as tk
master = tk.Tk()
def callback():
raise UserWarning("Exception!")
b = tk.Button(master, text="This will raise an exception", command=callback)
b.pack()
tk.mainloop()
However, if I run it without a terminal (say, by double-clicking an icon), the error message is suppressed.
In my real, more complicated Tkinter application, I like that the GUI is a little crash-resistant. I don't like that my users have a hard time giving me useful feedback to fix the resulting unexpected behavior.
How should I handle this? Is there a standard way to expose tracebacks or stderror or whatnot in a Tkinter application? I'm looking for something more elegant than putting try/except everywhere.
EDIT: Jochen Ritzel gave an excellent answer below that pops up a warning box, and mentioned attaching it to a class. Just to make this explicit:
import Tkinter as tk
import traceback, tkMessageBox
class App:
def __init__(self, master):
master.report_callback_exception = self.report_callback_exception
self.frame = tk.Frame(master)
self.frame.pack()
b = tk.Button(
self.frame, text="This will cause an exception",
command=self.cause_exception)
b.pack()
def cause_exception(self):
a = []
a.a = 0 #A traceback makes this easy to catch and fix
def report_callback_exception(self, *args):
err = traceback.format_exception(*args)
tkMessageBox.showerror('Exception', err)
root = tk.Tk()
app = App(root)
root.mainloop()
My remaining confusion: Jochen mentions the possibility of having different exception reporting functions in different frames. I don't yet see how to do that. Is this obvious?
There is report_callback_exception
to do this:
import traceback
import tkMessageBox
# You would normally put that on the App class
def show_error(self, *args):
err = traceback.format_exception(*args)
tkMessageBox.showerror('Exception',err)
# but this works too
tk.Tk.report_callback_exception = show_error
If you didn't import 'Tkinter as tk', then do
Tkinter.Tk.report_callback_exception = show_error
这篇关于我应该在tkinter中做出沉默的例外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!