Home > Back-end >  Stop resizing tkinter grid on button.config
Stop resizing tkinter grid on button.config

Time:10-22

I'm working on a project with a tkinter grid of 64 (8x8) square buttons. Every time a button is clicked, the text in it should change using config. However, when the text changes, the button reseizes so it can fit the text better, which I don't want because the squares aren't square anymore now. Here is my code for setting up one of the rows and columns:

root.columnconfigure(0, weight=3)

root.rowconfigure(1, weight=3)

And here is a screenshot of the not so square buttons:
Image

CodePudding user response:

Set a constant width for your Button widgets and apply it when creating the buttons with the width parameter, for example:

import tkinter as tk

BTN_WIDTH = 10

root = tk.Tk()
root.columnconfigure(0, weight=3)
root.rowconfigure(1, weight=3)

btn = tk.Button(root, width=BTN_WIDTH)
btn.grid(row=0, column=0)
  • Related