在Tkinter中,您可以使用`messagebox`模块来显示错误消息和traceback。以下是一个示例¹:
```python
import tkinter.messagebox as tm
import traceback
try:
1/0
except Exception as error:
tm.showerror(title="Error", message="An error has occurred: '" + str(error) + "'.", detail=traceback.format_exc())
```
这将显示一个包含错误消息和traceback的消息框。但是,这种方法有一些缺点,例如,traceback的详细信息对于普通用户来说可能并不有用,而且测试人员无法从消息框中轻松地选择和复制文本。
如果您希望在消息框中添加一个“显示详细信息”的按钮,可以创建自己的消息框¹。以下是一个示例,它创建了一个自定义的错误框,其中包含一个可以切换显示或隐藏详细信息的按钮¹:
```python
import tkinter as tk
import tkinter.ttk as ttk
import traceback
class MyApp(tk.Tk):
def __init__(self):
super().__init__()
tk.Button(self, text='test error', command=self.run_bad_math).pack()
@staticmethod
def run_bad_math():
try:
1/0
except Exception as error:
title = 'Traceback Error'
message = "An error has occurred: '{}'.".format(error)
detail = traceback.format_exc()
TopErrorWindow(title, message, detail)
class TopErrorWindow(tk.Toplevel):
def __init__(self, title, message, detail):
tk.Toplevel.__init__(self)
self.details_expanded = False
self.title(title)
self.geometry('350x75')
self.minsize(350, 75)
self.maxsize(425, 250)
self.rowconfigure(0, weight=0)
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
button_frame = tk.Frame(self)
button_frame.grid(row=0, column=0, sticky='nsew')
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
text_frame = tk.Frame(self)
text_frame.grid(row=1, column=0, padx=(7, 7), pady=(7, 7), sticky='nsew')
text_frame.rowconfigure(0, weight=1)
text_frame.columnconfigure(0, weight=1)
ttk.Label(button_frame, text=message).grid(row=0, column=0, columnspan=2, pady=(7, 7))
ttk.Button(button_frame, text='OK', command=self.destroy).grid(row=1, column=0, sticky='e')
ttk.Button(button_frame, text='Details', command=self.toggle_details).grid(row=1, column=1, sticky='w')
self.textbox = tk.Text(text_frame, height=6)
self.textbox.insert('1.0', detail)
self.textbox.config(state='disabled')
self.scrollb = tk.Scrollbar(text_frame, command=self.textbox.yview)
self.textbox.config(yscrollcommand=self.scrollb.set)
def toggle_details(self):
if self.details_expanded:
self.textbox.grid_forget()
self.scrollb.grid_forget()
self.geometry('350x75')
self.details_expanded = False
else:
self.textbox.grid(row=0, column=0, sticky='nsew')
self.scrollb.grid(row=0, column=1, sticky='nsew')
self.geometry('350x160')
self.details_expanded = True
if __name__ == '__main__':
App = MyApp().mainloop()
```
这个示例中的`TopErrorWindow`类创建了一个新的顶级窗口,该窗口包含一个标签(显示错误消息)、两个按钮(一个用于关闭窗口,另一个用于切换详细信息的显示)和一个文本框(显示traceback)。`toggle_details`方法用于切换文本框的显示。当用户点击“详细信息”按钮时,如果文本框当前是隐藏的,那么它将被显示,反之亦然¹。希望这个示例能对您有所帮助!
源: 与必应的对话, 2024/1/16
(1) How can I add a "show details" button to a tkinter messagebox?. https://stackoverflow.com/questions/49072942/how-can-i-add-a-show-details-button-to-a-tkinter-messagebox.
(2) python - How to create a tkinter error message box - Stack Overflow. https://stackoverflow.com/questions/47676319/how-to-create-a-tkinter-error-message-box.
(3) KevinLi-py/traceback-gui - GitHub. https://github.com/KevinLi-py/traceback-gui.
(4) How can I make silent exceptions louder in tkinter?. https://stackoverflow.com/questions/4770993/how-can-i-make-silent-exceptions-louder-in-tkinter.
(5) How to print the full traceback without halting the program?. https://stackoverflow.com/questions/3702675/catch-and-print-full-python-exception-traceback-without-halting-exiting-the-prog.
(6) github.com. https://github.com/RoseannaD/TeamSoftware/tree/b7d28467a7594401ee98cd475c2106e2d8250035/main.py.
(7) github.com. https://github.com/cjweaver/IngestFailer/tree/69fdc8f6906e122f963185b20afcae0f54172a35/ingestfailer%2F__main__.py.
标签:__,tkinter,展示,text,self,traceback,frame From: https://blog.51cto.com/u_16055028/9272731