简体   繁体   English

如何使 tkinter 的“扩展”在小部件之间给出相等的间隙?

[英]How to make tkinter's `expand` give equal gaps between widgets?

If you .pack(side=LEFT, expand=True) 3 widgets, you get this:如果你.pack(side=LEFT, expand=True) 3 个小部件,你会得到:

tkinter 不等间距

Image's source ;图片来源 same is true with .grid(...) . .grid(...)也是如此。

Notice that there are 4 vertical gaps: left, two in the middle, right.请注意,有 4 个垂直间隙:左侧,中间两个,右侧。 However, the gaps on left and right are half of those in the middle.但是,左右的差距是中间的一半。

My question is: how to make expand work more evenly, so that all of the four vertical gaps are equal?我的问题是:如何使expand工作更均匀,使四个垂直间隙都相等?

This is .pack 's man page in case it helps..pack的手册页,以防万一。

It can be done using .grid() .可以使用.grid()来完成。 You need to put those labels in column 1,3,5 and set weight=1, uniform=1 on column 0,2,4,6 using .columnconfigure() .您需要将这些标签放在第 1、3、5 列中,并使用.columnconfigure()在第 0、2、4、6 列上设置weight=1, uniform=1

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")

frm = tk.Frame(master=root, bg="yellow")
# set weight=1 and uniform=1 on column 0,2,4,6
frm.columnconfigure(index=(0,2,4,6), weight=1, uniform=1)

# put labels in column 1, 3, 5
for i in range(1, 6, 2):
    tk.Label(master=frm, text=i, bg="red", padx=40, pady=10).grid(row=0, column=i)

frm.pack(fill="both", expand=True)

root.mainloop()

Result:结果:

在此处输入图像描述

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

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