From this question: What magic prevents Tkinter programs from blocking in interactive shell? which is answered by @abarnert:
If stdin is a TTY, then, each time it tries to fetch a line with
input(), various bits of thecodemodule, the built-in REPL, etc., Python calls any installedPyOS_InputHookinstead of just reading from stdin.*What
Tkinterdoes is similar. It's more complicated because it has to deal with Windows, but on nix it's doing something pretty similar toreadline. Except that it's callingTcl_DoOneEventeach time through the loop.
I got riddled by this behavior. input isn't exactly the same as mainloop of tkinter, therefore, Tcl_DoOneEvent won't be called by input. It's understandable that input and mainloop behave similarly, but input calls PyOS_InputHook instead of Tcl_DoOneEvent.
At the interactive shell when running Python interactively, I use gnome-terminal, running this script:
#main.py
from tkinter import *
win = Tk()
Button(win, command=(lambda:print('pressed'))).pack(side=TOP)
executing the file:
$python3 -i main.py
>>> print("something") # I didn't press enter here
I get the window on screen and pressing the button 2 times this is what I get in the terminal:
>>> print("something")pressed
pressed
after pressing enter the window responded to events, in terminal again:
>>> print("something")pressed
pressed
something
>>> pressed
Of course, there's no input in main.py, but running Python interpreter interactively does demonstrate the point. Generally, how input and the interactive interpreter obviate the need to call Tk.mainloop()?
Edit:
It would be easier to grasp if someone could possibly show us the inner working of input and the interactive interpreter with pseudo-code or Python code.
This might seem to be a duplicate, I hope this question will complement the previous one and add more clarification to it.