0

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

2 Answers2

1

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

1

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")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I test it out and it worked perfectly! Thanks. I also noticed that I can solve the problem without using the uniform parameter: win.grid_columnconfigure((0,1,2), weight=1). – Carlos Pedro de O. dos S Jun 15 '21 at 04:54
  • @CarlosPedrodeO.dosS: yes, if the buttons default to being the same size, `uniform` doesn't add any extra value. If you had widgets of different sizes, `uniform` is necessary to force the columns to be the same width. – Bryan Oakley Jun 15 '21 at 13:10