I am working on tkinter project, which the user can't see the console. Is there a way the user can know an error has occurred in the program with out seeing the console.
Asked
Active
Viewed 321 times
0
-
You can replace the function "sys.excepthook" by your code which can show a window or write to a log file if an error occurs. – Michael Butscher Mar 27 '22 at 12:57
-
1@MichaelButscher: `sys.excepthook` won't always work depending on when the exception occurs. For example, when one happens in a callback function if won't be called. – martineau Mar 27 '22 at 17:06
-
@Nahom: Note the [answer](https://stackoverflow.com/a/71638411/355230) I just posted to the duplicate question. – martineau Mar 27 '22 at 17:06
2 Answers
0
messagebox.showerror()
You can use this code for showing error.

Ahmet Çavdar
- 1
- 3
-
The trick is getting something to call that when an exception occurs… – martineau Mar 27 '22 at 14:40
0
You can add try except
statement from the beginning to end of your program, and in except
you can use messagebox.showerror
to show error.
And for catching the Tkinter error check this answer
But if you want to catch Python errors too for example if index out of range error occur while running a for loop you use
# Note import files before the try statement
from tkinter import *
from tkinter import messagebox
import sys,traceback
def show_error(slef, *args):
err = traceback.format_exception(*args)
messagebox.showerror('Exception',err)
try:
root=Tk()
Tk.report_callback_exception = show_error
exec(input()) # Just used to throw error
a=Button(text='s',command=lambda: print(8/0)) # Just used to throw error
a.pack()
root.mainloop()
except BaseException as e:
messagebox.showerror('Exception',e)

Faraaz Kurawle
- 1,085
- 6
- 24
-
1
-
@martineau , yeah that was because of wrong name. i have fixed it, thanks for pointing out. – Faraaz Kurawle Mar 27 '22 at 14:49
-
-
@martineau , oh for me its working, i have tried it by dividing interger by 0 – Faraaz Kurawle Mar 27 '22 at 14:54
-
Same here, but it doesn't work for me. In my code a `Button` callback function divides by 0. – martineau Mar 27 '22 at 14:55
-
@martineau , that question is the exact answer, so should i delete this answer? – Faraaz Kurawle Mar 27 '22 at 15:13
-
Doesn't really matter one way or another now — so that's totally up to you. IMO it could be useful for someone to see your wrong answer and these comments. The subtlety is that exceptions in tkinter callbacks aren't affected by a `try` / `except` at a higher level (as currently shown in your answer). – martineau Mar 27 '22 at 15:22