How can I make silent exceptions louder in tkinter?

后端 未结 3 1666
花落未央
花落未央 2020-12-03 05:43

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         


        
3条回答
  •  悲哀的现实
    2020-12-03 06:07

    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.

提交回复
热议问题