【问题标题】:How to do Layout Management in Python tkinter如何在 Python tkinter 中进行布局管理
【发布时间】:2021-06-21 06:29:23
【问题描述】:

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

我得到了这个布局:

我想要这种布局:

我的图像 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()

【问题讨论】:

  • 你可能会发现this很有用。
  • grid() 似乎是一个更好的方法。

标签: python tkinter layout


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-12-24
    • 2013-05-03
    • 2014-03-24
    • 2014-01-08
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多