Question

Why, in this really basic code snippet, the data label (lb_data) is not appearing in its parent widget (fr_body), and instead appears in fr_header?

(The heights, widths and reliefs are just there to show the location of the Frame widgets)

fr_header=ttk.Frame(root,width=100,height=100,relief=GROOVE).grid(row=0,column=0)
lb_title=ttk.Label(fr_header,text="Title Title Title").grid(row=0,column=0)

fr_body=ttk.Frame(root,width=150,height=150,relief=SUNKEN).grid(row=1,column=0)
lb_data=ttk.Label(fr_body,text="Data").grid(row=0,column=0)
Was it helpful?

Solution

The .grid method returns None. So, fr_header is actually being assigned the value None. When you pass that to your label Tkinter sees the None and assumes you want to make the master widget the root widget (Tk()).

I never create a widget and grid at the same time as then you lose your easy handle on the widget you just created. Do it this way instead:

fr_header=ttk.Frame(root,width=100,height=100,relief=GROOVE)
fr_header.grid(row=0,column=0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top