0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 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 Answers2

0
messagebox.showerror()

You can use this code for showing error.

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