简体   繁体   中英

How to divide space equally between widgets using Grid Layout Python Tkinter

I want to divide space equally between three buttons using Grid Layout in Tkinter. And those buttons should occupy the space equally even when the window is resized horizontally.

################################
#|  btn1  ||  btn2  ||  btn3  |#
################################

Here's my code:

import tkinter as tk
from tkinter import ttk

win = tk.Tk()
win.grid_columnconfigure(0, weight=1)

btn1 = ttk.Button(win, text='btn1')
btn1.grid(column=0, row=0, columnspan=1, sticky='EW')

btn2 = ttk.Button(win, text='btn2')
btn2.grid(column=1, row=0, columnspan=1, sticky='EW')

btn3 = ttk.Button(win, text='btn3')
btn3.grid(column=2, row=0, columnspan=1, sticky='EW')

win.mainloop()

Does this help?

import tkinter as tk
from tkinter import Grid, ttk

win = tk.Tk()

btn1 = ttk.Button(win, text='btn1')
btn1.grid(column=0, row=0, columnspan=1, sticky='EW')

btn2 = ttk.Button(win, text='btn2')
btn2.grid(column=1, row=0, columnspan=1, sticky='EW')

btn3 = ttk.Button(win, text='btn3')
btn3.grid(column=2, row=0, columnspan=1, sticky='EW')
Grid.columnconfigure(win,0, weight=1)
Grid.columnconfigure(win,1, weight=1)
Grid.columnconfigure(win,2, weight=1)


win.mainloop()

If you want space, you can add padx=5 to btn1.grid() and btn3.grid()

Use the uniform parameter to give all three columns a uniform width.The value can be anything as long as it's the same for all columns that are to be the same size. If you want them to grow as the window is resized, be sure to give them all a non-zero weight.

win.grid_columnconfigure((0,1,2), weight=1, uniform="column")

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