首页 > 解决方案 > 与按钮不同的列中的 Tkinter 包装条目

问题描述

我正在使用一个简单的代码:

frame_bottom = Frame(root)
frame_bottom.pack(fill='x')

button1 = Button(frame_bottom, text = 'One', command =  visualize)
button2 = Button(frame_bottom, text = 'Two', state = DISABLED)
label_rango1 = Label(frame_bottom, text = 'Sigma value:')
e_sigma = Entry(root, borderwidth = 2)

label_rango1.pack(side='left',  expand=True)
e_sigma.pack(side='left')
button1.pack(side='left',  expand=True)
button2.pack(side='left', expand=True)  

然而,尽管使用side='left',该条目仍位于其余按钮下方。关于如何将所有内容打包在同一行中的任何想法?

谢谢!

标签: pythontkintertkinter-entrypack

解决方案


原因是因为button1andbutton2是 的孩子frame_bottom,但是e_sigma是 的孩子rootframe_bottom您之前打包e_sigma并且不提供任何选项,因此frame_bottom被添加到根窗口的顶部(好吧,可用空间的顶部,如果您已经在根窗口中打包了其他小部件,这很重要)。当你打包e_sigma时,它必须放在下面定义的剩余空间中frame_bottom

换句话说,frame_bottom并且e_sigma都是 的孩子rootframe_bottom首先打包,然后e_sigma打包,都带有默认选项。因此,frame_bottom会出现在上面e_sigma

如果您希望条目位于按钮的右侧,则它需要与按钮具有相同的父级。


推荐阅读