Search code examples
python-3.xtkintertkinter-label

Tkinter Widgets without Parent Master


What are the consequences when omitting the parent master of a widget? Those two programs do not produce a visual difference:

import tkinter as t
import tkinter.ttk as ttk

w = t.Tk()
w.title("Label 1")

label_1 = ttk.Label(text="Label")
label_1.grid(row=0, column=0)

fenster.mainloop()

vs.

import tkinter as t
import tkinter.ttk as ttk

w = t.Tk()
w.title("Label 1")

label_1 = ttk.Label(w, text="Label")
label_1.grid(row=0, column=0)

fenster.mainloop()

Solution

  • When you ommit the master option it will use the root widget.
    Which is the Tk() instance or in Tkinter the root widget '.'

    You can get parent with winfo_parent() methode.

    import tkinter as t
    import tkinter.ttk as ttk
    
    w = t.Tk()
    w.title("Label 1")
    
    label_1 = ttk.Label(text="Label")
    label_1.grid(row=0, column=0)
    print("parent =", label_1.winfo_parent())
    w.mainloop()
    

    Output:

    parent = .