Tkinter button expand using grid Tkinter button expand using grid tkinter tkinter

Tkinter button expand using grid


Question: Tkinter button expand using grid

TkDocs: The Grid Geometry Manager Handling Resize


You are configure only the root to grow:

root.grid_columnconfigure(0, weight=1)root.grid_rowconfigure(0, weight=1)

You have to configure the keyboard frame also to grow if there is extra room, add:

keyboard.grid_columnconfigure(0, weight=1)keyboard.grid_rowconfigure(0, weight=1)

You have to configure every keyboard row/column with weight=1.

You don't set the keyboard frame to expand

keyboard.grid(row=0,column=0)

change to

keyboard.grid(row=0,column=0, sticky=NSEW)

Working example:

root=Tk()root.geometry("500x500")root.grid_columnconfigure(0, weight=1)root.grid_rowconfigure(0, weight=1)keyboard=Frame(root,padx=5,pady=5,bg="red")keyboard.grid(row=0,column=0, sticky=NSEW)for row, row_buttons in enumerate([['%', '√x', 'x²', '1/x'], ["C", "←", "÷"]]):    keyboard.grid_rowconfigure(row, weight=1)    for col, text in enumerate(row_buttons):        keyboard.grid_columnconfigure(col, weight=1)        Button(keyboard, padx=5, pady=5, text=text).grid(row=row, column=col, sticky='EWNS')

enter image description here

Tested with Python: 3.5 - TkVersion: 8.6