Skip to content

Nested tkinter mainloop() resets the dispatching flag of the outer mainloop() #162650

Done

Description

@serhiy-storchaka

mainloop() sets a flag which tells that the interpreter dispatches events, so that calls from other threads can be passed to it. But a nested mainloop() resets this flag when it exits, even if the outer mainloop() is still running. After that, calls from other threads fail with RuntimeError: main thread is not in main loop.

A nested mainloop() is run, for example, by SimpleDialog.go() in tkinter.simpledialog and FileDialog.go() in tkinter.filedialog.

import threading, tkinter

root = tkinter.Tk()

def call_from_thread():
    try:
        print(root.tk.call('expr', '6*7'))
    except RuntimeError as e:
        print('RuntimeError:', e)

def start():
    root.after(100, root.quit)  # stop the nested loop
    root.mainloop()             # nested loop, as in SimpleDialog.go()
    threading.Thread(target=call_from_thread).start()
    root.after(2000, root.quit)

root.after(0, start)
root.mainloop()

Output:

RuntimeError: main thread is not in main loop

mainloop() should restore the previous value of the flag when it exits.

Linked PRs

Activity

  1. added
    type-bug
    3.14
    3.15
    3.16
    on Nov 26, 2026
  2. added a commit that references this issue on Nov 26, 2026
  3. added 2 commits that reference this issue on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.143.153.16topic-tkintertype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions