If you .pack(side=LEFT, expand=True)
3 widgets, you get this:
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.