简体   繁体   中英

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

If you .pack(side=LEFT, expand=True) 3 widgets, you get this:

tkinter 不等间距

Image's source ; same is true with .grid(...) .

Notice that there are 4 vertical gaps: left, two in the middle, right. 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?

This is .pack 's man page in case it helps.

It can be done using .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() .

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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