This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients terry.reedy
Date 2017-12-04.01:47:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
import tkinter as tk
root = tk.Tk()

def bad(): print(a, 'bad')
def good(): print('good')

root.after(1000, bad)
root.after(1500, good)

# Correctly gives this traceback and output:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python37\lib\tkinter\", line 1699, in __call__
    return self.func(*args)
  File "C:\Programs\Python37\lib\tkinter\", line 745, in callit
  File "F:\Python\a\", line 13, in bad
    def bad(): print(a, 'bad')
NameError: name 'a' is not defined

Remove or comment-out the blocking 'root.mainloop()' call and run the result from an IDLE editor.  The callbacks are still called because after user code is run, calls tcl.update in a loop nominally 20 x per second.  This allows developers to interact with a 'live' gui by entering statements in the shell at '>>>' prompts.  The result is this.
>>> Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python37\lib\idlelib\", line 137, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Programs\Python37\lib\", line 169, in get
    raise Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
<The NameError traceback and message, as before.>

The relevant code in was written before callback chaining.

                seq, request = rpc.request_queue.get(block=True, timeout=0.05)
            except queue.Empty:

Experiments with normal exceptions in a shell suggest that wrapping handle_tk_events in try:except and re-raising any exception 'from None' should work.
                except BaseException as e:
                    raise e from None

However, it appears that callback errors resulting from handle_tk_events() are not being caught here.  (print('message') and 1/0 before 'raise' have no visible effect.)  I will investigate more later.
Date User Action Args
2017-12-04 01:47:10terry.reedysetrecipients: + terry.reedy
2017-12-04 01:47:09terry.reedysetmessageid: <>
2017-12-04 01:47:09terry.reedylinkissue32207 messages
2017-12-04 01:47:04terry.reedycreate