How can I make silent exceptions louder in tkinter?

First a followup: Just today, a patch on the CPython tracker for the tkinter.Tk.report_callback_exception docstring made it clear that Jochen's solution is intended. The patch also (and primarily) stopped tk from crashing on callback exceptions when run under pythonw on Windows.

Second: here is a bare-bones beginning of a solution to making stderr function with no console (this should really be a separate SO question).

import sys, tkinter

root = tkinter.Tk()

class Stderr(tkinter.Toplevel):
    def __init__(self):
        self.txt = tkinter.Text(root)
        self.txt.pack()
    def write(self, s):
        self.txt.insert('insert', s)

sys.stderr = Stderr()

1/0 # traceback appears in window

More is needed to keep the popup window hidden until needed and then make it visible.


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