Which code does the mainloop() processes infinitely until any event occurs?

To understand my question kindly follow the paragraphs written below:

What code does the mainloop processes infinitely? Like does it read the code of the entire program again and again? consider the code:

from tkinter import *

window = Tk()
print("lol")
print("Hello World")
window.mainloop()

the output didn't print "Hello World" or "lol" infinite number of times, so the mainloop() doesn't loop the code of the current module.

Now consider this code:

from tkinter import *
print("lol")
window = Tk()
print("Hello World")
while True:
    window.update()

Now, even this code executes the same output, so now we can consider the mainloop() loops the code "window.update()" infite number of times, but more efficiently(somehow).

Now the first question arises what does the window.update() function do to update the values in the GUI, does it re-read the code from top to bottom again, or how does the update function update the GUI widget vaules.

The second question is :

I read this article

"Mainloop in Python Tkinter is an infinite loop of the application window which runs forever so that we can see the still screen. The application window is like a frame that keeps on destroying every microsecond but the main loop keeps on creating a new updated window. This process of destroying old window screens and creating a new one happens so fast that human eyes don’t even realize it. Since the process runs infinite time that is why we are able to see the application in front of us and when we close the window then the loop terminates or exits."

Now if this is true then to recreate an updated window the root.mainloop() must read the entire root GUI code again and again entirely or is there another explanation to it.

I have been trying to understand this for the past 6hrs and I have visited every site and I cannot find the solution for the life of me.

Regards, Rashik


Solution 1:

What code does the mainloop processes infinitely? Like does it read the code of the entire program again and again?

No.

Via this function, it calls this C code which has the embedded Tcl interpreter process one event, or wait for Tkinter_busywaitinterval before trying to process another event

Now, even this code executes the same output, so now we can consider the mainloop() loops the code "window.update()" infite number of times, but more efficiently(somehow).

window.update() calls TCL update, which is described to

[...] bring the application “up to date” by entering the event loop repeatedly until all pending events (including idle callbacks) have been processed.

Your infinite loop doesn't have a sleep, so it's spinning your CPU as hard as possible to do practically nothing.

[...] Does it re-read the code from top to bottom again, or how does the update function update the GUI widget vaules.

It certainly doesn't re-read your code. It processes any pending widget updates, which may have happened by running e.g. window.text("...") in e.g. a click callback or an .after() timeout, etc.

I read this article [...]

That article seems wrong and/or at least over-simplifies things.

This simple example clock should clarify how things work:

import time
import tkinter as tk

root = tk.Tk()
text = tk.Label(root)
text.pack()

def tick():
    text["text"] = time.ctime()  # update `text` widget's content
    root.after(1000, tick)  # schedule for this function to be called after 1 second


if __name__ == '__main__':
    tick()  # call the `tick` function once before entering main loop
    root.mainloop()