tkinter LabelFrame 不附加小部件 [英] tkinter LabelFrame not attatching widgets
问题描述
我在使用 tkinter 程序时遇到问题,我让 LabelFrame 对一组标签和条目进行分组,但是,它没有对我的小部件进行分组.我的 LabelFrame 代码如下:
I am running into a problem with a tkinter program, I have the LabelFrame grouping a set of labels and entries, however, it is not grouping my widgets. My code for the LabelFrame is as follows:
(我设法让标签显示,但是,它没有对我的小部件进行分组.)
(edit: i managed to get the Label to display, however, it is not grouping my widgets.)
root=Tk()
message_frame=LabelFrame(root,text="testing",padx=0,pady=0,width=100,height=100).grid(padx=5,pady=10)
message_label=Label(message_frame,text="Message").grid(row=1,column=0,sticky=W)
pub_label=Label(message_frame,text="Public Key").grid(row=2,column=0,sticky=W)
priv_label=Label(message_frame,text="Public Key").grid(row=3,column=0,sticky=W)
message_entry=Entry(message_frame,textvariable=message,width=50).grid(row=1,column=1,sticky=W)
pub_entry=Entry(message_frame,textvariable=pub_key,width=50).grid(row=2,column=1,sticky=W)
priv_entry=Entry(message_frame,textvariable=private_key,width=50).grid(row=3,column=1,sticky=W)
推荐答案
在 Tkinter 中,典型的工作流程是创建一个小部件,然后使用一些几何管理器将它放在两个单独的行上.
In Tkinter, the typical workflow is to create a widget and then place it using some geometry manager on two separate lines.
如果我没记错的话,Tkinter 小部件上的 .grid
方法返回 None
.因此,如果您在创建后立即打印 message_frame
,您可能会看到它是 None
.当你使用传递给下一个小部件时,他们假设你想把它放在根小部件上......
If I'm not mistaken, the .grid
method on Tkinter widgets returns None
. So if you print message_frame
right after you create it, you will probably see that it is None
. When you use that passed to the next widgets, they assume you want to put it on the root widget...
简单的解决方法是执行以下操作:
The easy fix is to do something like:
message_frame=LabelFrame(root,text="testing",padx=0,pady=0,width=100,height=100)
message_frame.grid(row=0,column=0)
你可能想对所有小部件都做同样的事情,因为我怀疑你真的想要 pub_label
= priv_label
= None
...
And you probably want to do the same with all the widgets since I doubt you actually want pub_label
= priv_label
= None
...
这篇关于tkinter LabelFrame 不附加小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!