Home > OS >  Tkinter Widgets without Parent Master
Tkinter Widgets without Parent Master

Time:03-29

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()

CodePudding user response:

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 = .
  • Related