What does calling Tk() actually do?

前端 未结 3 1738
天命终不由人
天命终不由人 2020-12-28 08:50

I was brushing up on Tkinter when I looked upon a minimal example from the NMT Tkinter 8.5 Reference.

#!/usr/bin/env python
import tkinter as tk

class Appli         


        
相关标签:
3条回答
  • 2020-12-28 09:17

    Bryan Oakley's answer is spot on. Creating a widget will implicitly create an instance of the tcl/tk interpreter. I would, however, like to add some pieces of code, to better understand how Tk is implicitly created.

    Whenever a Widget object is created (whether it is a Frame or a Button or even a ttk-based widget), the BaseWidget class' __init__ method is called, which in turn calls the _setup method. Here is a snippet of the relevant part:

    def _setup(self, master, cnf):
        """Internal function. Sets up information about children."""
        if _support_default_root:
            global _default_root
            if not master:
                if not _default_root:
                    _default_root = Tk()
                master = _default_root
        self.master = master
        self.tk = master.tk
    

    Both _support_default_root and _default_root are global variables, declared in lines 132-133 of the __init__.py file in the tkinter package. They are initialized to the following values:

    _support_default_root = 1
    _default_root = None
    

    This means that, if master isn't provided, and if an interpreter wasn't already created, an instance of Tk gets created and assigned as the default root for all future widgets.

    There is also something interesting when creating an instance of the Tk class. The following snippet comes from the Tk._loadtk method:

    if _support_default_root and not _default_root:
        _default_root = self
    

    Which means that, regardless of how the Tk class gets initialized, it is always set up as the default root.

    0 讨论(0)
  • 2020-12-28 09:25

    Tkinter works by starting a tcl/tk interpreter under the covers, and then translating tkinter commands into tcl/tk commands. The main window and this interpreter are intrinsically linked, and both are required for a tkinter application to work.

    Creating an instance of Tk initializes this interpreter and creates the root window. If you don't explicitly initialize it, one will be implicitly created when you create your first widget.

    I don't think there are any pitfalls by not initializing it yourself, but as the zen of python states, "explicit is better than implicit". Your code will be slightly easier to understand if you explicitly create the instance of Tk. It will, for instance, prevent other people from asking the same question about your code that you just asked about this other code.

    0 讨论(0)
  • 2020-12-28 09:40

    Sometimes some widgets run Tk() on their own. But I don't know which widgets and why.

    Sometimes it runs even if you don't use mainloop() - mostly on Windows.

    Tkinter is strange wrapper on Tcl/Tk :)

    But I prefer to use root = Tk() and root.mainloop() always.

    0 讨论(0)
提交回复
热议问题