Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the n parameter of tkinter.mainloop function?

Tags:

python

tkinter

A n parameter may be given to tkinter.mainloop function,

help(tkinter.Tk.mainloop)
>>>> mainloop(self, n=0) # What is n here ?
     Call the mainloop of Tk.

I was not able to find any documentation about it

What is the purpose of this n parameter?

like image 445
ismailarilik Avatar asked Jul 17 '18 17:07

ismailarilik


People also ask

What does Mainloop () do in tkinter?

mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.

What is Mainloop function?

mainloop(): There is a method known by the name mainloop() is used when your application is ready to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event as long as the window is not closed. m.mainloop() import tkinter. m = tkinter.Tk()

What is root Mainloop tkinter?

How the mainloop work in Tkinter? Root. mainloop() is simply a method in the main window that executes what we wish to execute in an application (lets Tkinter to start running the application). As the name implies it will loop forever until the user exits the window or waits for any events from the user.

How can I tell if tkinter Mainloop is running?

If you know the process name, in Windows, you can use psutil to check if a process is running. if __name__ == "__main__": app_name = 'chrome' if is_running(name=app_name): if kill(name=app_name): print(f'{app_name} killed!


1 Answers

As you can see in the C implementation of Tkinter , _tkinter_tkapp_mainloop_impl,

_tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)

n represent the threshold parameter passed to the function.

Now, looking at the implementation itself, it is possible to see this loop at the beginning of the function,

 while (Tk_GetNumMainWindows() > threshold &&
       !quitMainLoop &&
       !errorInCmd)

Hence, you can see that the code is meant to drop out of the mainloop when the number of root level windows drops to threshold or below.

Note that by default the optional parameter will have a value of 0 which logically means it will stay active if any root level windows are opened.

Further information

I can't comment on why this threshold parameter was added, but the lack of documentation and/or information on this specific parameter most likely comes from the fact that it seems quite rare that someone would pass n explicitly to tkinter.mainloop and change the default behavior.

like image 168
scharette Avatar answered Oct 17 '22 11:10

scharette