如何在Python tkinter中进行布局管理

2024-06-14 11:43:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我附上了两张图片,显示了我当前获得的布局(图片1)和我想要的布局(图片2),(绿色框位置的标签3和紫色框位置的条目1)。我可以使用pack()获得图像2的输出,还是需要grid()place()

我得到这个布局:

enter image description here

我想要这种类型的布局:

enter image description here

我的图像1代码[我目前正在获取]

from tkinter import *

root = Tk()
root.geometry('1000x700+0+0')
root.minsize('1000','700')

f1 = Frame(root, bd=5, relief=GROOVE, width=500, height=500)
f1.pack(fill=BOTH, expand=True, padx=30, pady=30)

f2 = Frame(f1, bg='pink', bd=5, relief=GROOVE)
f2.place(anchor=CENTER, relx=0.5, rely=0.5, relwidth=0.8, relheight=0.8)

lbl_1 = Label(f1, text='Label 1',  bg='yellow', bd=2.5, relief=GROOVE,  font=('Times New Roman',20,'bold'))
lbl_1.pack(side=TOP, pady=5)

lbl_2 = Label(f2, text='Label 2',  bg='yellow', bd=2.5, relief=GROOVE,  font=('Times New Roman',20,'bold'))
lbl_2.pack(side=TOP, pady=5)

lbl_3 = Label(f2, text='Label 3', bg='yellow', bd=2.5, relief=GROOVE,  font=('Times New Roman',15,'bold'))
lbl_3.pack(pady=10)

txt_1 = Entry(f2, text='Txt 1', bd=2.5, relief=SUNKEN,  font=('Times New Roman',15,'bold'))
txt_1.pack(pady=10)

lbl_4 = Label(f2, text='Label 4',  bg='yellow', bd=2.5, relief=GROOVE,  font=('Times New Roman',20,'bold'))
lbl_4.pack(side=BOTTOM, pady=5)

root.mainloop()

Tags: textnewrootlabelbdpackf2bg
1条回答
网友
1楼 · 发布于 2024-06-14 11:43:45

需要在.pack(...)中使用anchor="w"将标签和条目移动到左侧,并使用padx添加水平边距:

...
lbl_3.pack(padx=30, pady=10, anchor="w")
...
txt_1.pack(padx=30, pady=10, anchor="w")
...

相关问题 更多 >