Python3, Understanding the "GUI loop" as in tkinter-mainloop() or QtPy-exec_() Python3, Understanding the "GUI loop" as in tkinter-mainloop() or QtPy-exec_() tkinter tkinter

Python3, Understanding the "GUI loop" as in tkinter-mainloop() or QtPy-exec_()


[1] GUIs are polling based?

GUIs are event based -- everything that happens in a GUI is a response to an event. How the application handles the event is up to the application. If the handling of the event can happen in a few hundred milliseconds or less it can be handled in the main thread. If it is going to take longer, the application should run the code in a separate thread or process.

In the case of Tkinter, Tkinter is designed to run in a single thread. That doesn't mean you can't use threads, only that all access to tkinter objects should be on a single thread. The common way to do this is for other threads to communicate with the GUI via a thread-safe queue, and the GUI thread is responsible for checking that queue periodically.

So my question is, Is there an option like [root.setInterruptMode(True)]

In tkinter, no, there is no way to enter a special "interrupt" mode.

[2] It is just a gigantic loop?

I assume by "it" you mean the call to mainloop. Yes, it's a gigantic loop. Or perhaps more correctly, it's a tiny loop. All it does is wait for an event, then it looks up the handler for the event and runs the handler.

My impression is that mainloop() doesn't really start a thread or anything in python

Correct. It runs the event loop in the current thread.

[3] why putting mainloop in Main line?

You would have to ask the original developers that. Likely it's because that's all that is necessary for a very, very large percentage of applications (perhaps more true back when the toolkit was invented than it is now).

Tkinter is nothing but a wrapper around a tcl interpreter with the tk toolkit loaded in the interpreter. tcl/tk was designed to be embedded in other applications, so it needed to be lightweight, and be able to run on systems that don't support threads (tcl is old enough that thread support wasn't guaranteed on every platform)

Does mainloop() have to reside in the Main line? can I thread/multiprocess it out?

mainloop() needs to be run in the same thread that created the root window. If you need threads (and again, it's quite possible that you don't), you can certainly run your event handlers in a separate thread. It adds complexity, but it's there if you need it. For a large class of programs, there's simply no need for that much complexity.

You should be able to create a worker thread and create and run your GUI from there. I've never tried it, but I see no reason why it wouldn't work. Tkinter doesn't care that it's the main thread, only that all tkinter code runs in the same thread.

All the examples I came across have mainloop() in the Main line, I am not sure it is a recommended approach or what the benefits are.

Calling mainloop in the main thread is how tkinter was designed to work. If you have long running calculations, you should put those calculations in a separate thread or process.