Search code examples
pythontkintergrid-layouttkinter-layouttkinter-button

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()


Solution

  • 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")