简体   繁体   English

如何在Tkinter的另一个框架内添加框架?

[英]How to add an Frame inside another frame in Tkinter?

I am trying to add a frame inside the another frame using tkinter, but the frame is not added: 我正在尝试使用tkinter在另一个框架内添加框架,但未添加框架:

Code: 码:

lframe=Frame(r,width=1000,height=650,relief='raise',bd=8).pack(side=LEFT)

rframe=Frame(r,width=350,height=650,relief='raise',bd=8).pack(side=RIGHT)



lframe1=Frame(lframe,width=1000,height=100,bd=8,relief='raise').pack(side= TOP)

lframe2=Frame(lframe,width=1000,height=55,bd=8,relief='raise').pack(side=TOP)

rframe1=Frame(rframe,width=350,height=215,relief='raise',bd=8).pack(side=TOP)

rframe2=Frame(rframe,width=350,height=415,relief='raise',bd=8).pack(side=TOP)

r.mainloop()

its shows the following output 它显示以下输出

产量

What caused your problem? 是什么引起了您的问题?

lframe is worth None and does not refer to the frame instance you created in anyway. lframe的价值为“ None ,并且无论如何都不会引用您创建的框架实例。 Why so? 为什么这样?

Because pack() , gird() and place() layout managers are functions that return nothing. 因为pack()gird()place()布局管理器是什么都不返回的函数。 So when you write: 所以当你写:

lframe=Frame(r,width=1000,height=650,relief='raise',bd=8).pack(side=LEFT)

You are clearly getting None from pack(side=LEFT) 您显然从pack(side=LEFT)获得了None

How to fix your problem? 如何解决您的问题?

Simply follow this principle whenever you create a widget: create then position (In your case, you create and position the widget on the same time). 创建小部件时,只需遵循以下原则:先创建然后定位(在您的情况下,您同时创建并定位小部件)。 I personally refer to this principle as The SaYa idiom . 我个人将此原则称为SaYa习语 This means the previous line of code should be written as follows: 这意味着上一行代码应编写如下:

lframe=Frame(r,width=1000,height=650,relief='raise',bd=8)
lframe.pack(side=LEFT)

Apply this idiom to all your widgets and that will save you from some troubles. 将此惯用语应用于所有小部件,可以避免麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM