Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Button Alignment in Grid

I am attempting to fit two buttons on a grid within a frame, that takes up the entire row, no matter the size of the root frame. So essentially one button takes up half of the row, while the other takes the other half. Here's my code:

self.button_frame = tk.Frame(self)
self.button_frame.pack(fill=tk.X, side=tk.BOTTOM)

self.reset_button = tk.Button(self.button_frame, text='Reset')
self.run_button = tk.Button(self.button_frame, text='Run')

self.reset_button.grid(row=0, column=0)
self.run_button.grid(row=0, column=1)

Not really sure where to go from here. Any suggestions would be greatly appreciated. Thanks!

like image 933
KidSudi Avatar asked Feb 14 '23 10:02

KidSudi


1 Answers

Use columnconfigure to set the weight of your columns. Then, when the window stretches, so will the columns. Give your buttons W and E sticky values, so that when the cells stretch, so do the buttons.

import Tkinter as tk

root = tk.Tk()

button_frame = tk.Frame(root)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)

reset_button = tk.Button(button_frame, text='Reset')
run_button = tk.Button(button_frame, text='Run')

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)

reset_button.grid(row=0, column=0, sticky=tk.W+tk.E)
run_button.grid(row=0, column=1, sticky=tk.W+tk.E)

root.mainloop()

Result:

enter image description here

like image 143
Kevin Avatar answered Feb 16 '23 03:02

Kevin