2

I have tried to find some Q/A or article about the use of tk.mainloop() vs root.mainloop() with no success.

My question is this: Is there any difference between the 2 uses. It seams to me the correct method would be to use tk_instance_variable_name.mainloop() vs just doing tk.mainloop() but from what I can see both appear to work just fine. Is there any reason one would need to avoid tk.mainloop() or is it just a preference.

If this has been asked before please provide the Q/A link as I cannot seam to find it. I feel like it would have been asked already but no luck search for it.

Can someone maybe explain why tk.mainloop() will work here when I feel like it should not work as it is not being used on the tk instance variable name.

Example using root work just as expected:

import tkinter as tk

root = tk.Tk()
tk.Label(root, text="Test").pack()
root.mainloop() # using the variable name root

Example using tk works fine as well as far as I can tell:

import tkinter as tk

root = tk.Tk()
tk.Label(root, text="Test").pack()
tk.mainloop() # using tk
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • No, there's no difference. `tk.mainloop()` is a helper function that looks up the root instance and calls `root.mainloop()`. [See it here.](https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Lib/tkinter/__init__.py#L555). – Novel Jul 19 '18 at 17:06
  • @Novel I see you linked to what looks like source code but I still don't understand how `tk.mainloop()` knows to loop on `root` when its not being told to. Also wouldn't this be a problem for multiple instances of tk. There are some case uses for having more than one instance of tk up so would `tk.mainloop()` fail in this case? – Mike - SMT Jul 19 '18 at 17:09
  • 3
    When you create a `Tk()` instance, the global variable `tkinter._default_root` is set if it isn't already. Therefore _default_root is the first instance of `Tk()` created. [Code here](https://github.com/python/cpython/blob/master/Lib/tkinter/__init__.py#L2055). If you have multiple instances of `Tk()` (which you shouldn't) then `tkinter.mainloop()` is an alias to the `mainloop()` method of the first `Tk()` instance you created. – Novel Jul 19 '18 at 17:14

2 Answers2

6

I have tried to find some Q/A or article about the use of tk.mainloop() vs root.mainloop() with no success.

My question is this: Is there any difference between the 2 uses.

Short answer: there is no difference in the normal use case.

Every widget has an associated tcl interpreter that is created when a root widget is created, whether either explicitly or implicitly. When you call mainloop from any widget, it will run the mainloop function in the interpreter associated with the root window of that widget.

If you call the mainloop method that is part of the tkinter module (eg: tk.mainloop() in your example), it will call the mainloop function of the default interpreter. The default interpreter is the first interpreter that was created. Thus, in the normal case of a single instance of Tk, tk.mainloop() and root.mainloop() call the exact same code.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • OP is asking about the `tkinter.mainloop()` function; not a widget method. – Novel Jul 19 '18 at 17:16
  • I realize having 2 active instance if TK is unlikely. I just wanted to cover all the options. I guess I am still trying to rap my head around it. So from what you have said and what @Novel has said let me see if I get this. So no mater how I call `mainloop()` (via variable name or directly against the tk import) then the mainloop() functions should look up the default instance and loop on that. – Mike - SMT Jul 19 '18 at 17:18
  • @Mike-SMT: No. If called on a widget, it will call the mainloop function for the interpreter associated with that widget. If you call the module-level one, it will call it on the first interpreter that was created. I'll try to clarify my answer. – Bryan Oakley Jul 19 '18 at 17:19
  • 1
    @BryanOakley the big difference is that `tkinter.mainloop()` has no way to know which `Tk()` instance / tcl interpreter to use, so it uses the first one created. – Novel Jul 19 '18 at 17:20
  • Thanks Bryan. I think I see whats going on here. I didn't realize `mainloop()` was coded in such a way to work on the default interpreter. My original understanding was that `mainloop()` needed to be used on the instance of tk directly to work. Learn something new every day. – Mike - SMT Jul 19 '18 at 17:28
1

BTW, every tkinter widget inherits that method, so this works too:

import tkinter as tk

lbl = tk.Label(text="Test")
lbl.pack()
lbl.mainloop()
Novel
  • 13,406
  • 2
  • 25
  • 41