anandhu
anandhu

Reputation: 760

Python tkinter - Unable to position child frame while using nested frames

I was experimenting with tkinter,

#Created black frame

frame=Frame(root, width=366, height=559,bg="black")
frame.pack(padx=(0,0),pady=(0,0))

#Placed grey frame and magenta frame ON black frame

frame2=Frame(frame, width=349, height=57,bg="grey").place(x=8, y=10)
frame3=Frame(frame, width=70, height=473,bg="magenta").place(x=8, y=74)

#Placing Blue frame ON magenta frame, at (0,0), but this goes to the root's (0,0)

frame4=Frame(frame3, width=50, height=50,bg="blue").place(x=0, y=0)

The grey and magenta frames are perfectly placed relative to the the black frame But the blue frame goes to the root window's (0,0). Why? How will i place blue frame on the magenta frame?

enter image description here

Full code:

from tkinter import *
root = Tk()
root.title("Fee Fie Foe Fum")
frame=Frame(root, width=366, height=559,bg="black")
frame.pack(padx=(0,0),pady=(0,0))
frame2=Frame(frame, width=349, height=57,bg="grey").place(x=8, y=10)
frame3=Frame(frame, width=70, height=473,bg="magenta").place(x=8, y=74)
frame4=Frame(frame3, width=50, height=50,bg="blue").place(x=0, y=0)
root.mainloop()

Upvotes: 0

Views: 195

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

The blue frame frame4 goes into the root window because it uses frame3 as a parent. frame3 is None, which tkinter treats the same as if you had passed the root window.

The reason frame3 is None is because place (and pack and grid) all return None. Threfore, Frame(...).place(...) returns None.

IMHO, it's always best to separate widget creation from widget layout. It results in the need for some temporary variables, but using a single style (always separating the two) makes the code easier to manage than sometimes using one style and sometimes using the other.

Upvotes: 1

Related Questions