What actually causes the call to Tk ()?

I sneaked up on Tkinter when I looked at the minimal example from the NMT Tkinter 8.5 Reference .

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

class Application(tk.Frame):    
    def __init__(self, master=None):    
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):    
        self.quitButton = tk.Button(self, text='Quit',command=self.quit)
        self.quitButton.grid()

app = Application()
app.master.title('Sample application')
app.mainloop()

Everything is fine and good, until I noticed that the class is Tknot initialized. In other online reference materials, I could find ( Python Library Reference , effbot.org , TkDocs ) a call usually appears root = tk.Tk(), from which the rest of the examples are built. I also did not see any class initialization Tkreference anywhere in the NMT reference. A.

, Tk, , Python Reference, " ... ". , , :

root = tk.Tk()
app = Application(root)

, . , :

  • root = tk.Tk() ( , ) ?
  • , Tk() Frame?
+4
3

Tkinter , tcl/tk , tkinter tcl/tk. , tkinter.

Tk . , .

, - , , python, " , ". , Tk. , , , .

+6

. tcl/tk. , , , , Tk .

, Widget ( ttk), BaseWidget class '__init__, , , _setup. :

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

_support_default_root _default_root , 132-133 __init__.py tkinter. :

_support_default_root = 1
_default_root = None

, master , , Tk .

- Tk. Tk._loadtk:

if _support_default_root and not _default_root:
    _default_root = self

, , Tk, .

+3

Tk() . , .

, mainloop() - Windows.

Tkinter - Tcl/Tk:)

root = Tk() root.mainloop().

-3

Source: https://habr.com/ru/post/1548295/


All Articles