문제

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)
도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top