I want to make all the tkinter buttons the same size regardless of text. Is it possible to stretch other buttons to match each other or set a specific size? As I am having difficulty finding how to do so in the documentation. Currently the buttons stretch based on the size of the text. Example of what I mean. Is it possible to make them all the same size?
Asked
Active
Viewed 1.1k times
2 Answers
11
You would typically do this when you use a geometry manager (pack
, place
, or grid
).
Using grid:
import tkinter as tk
root = tk.Tk()
for row, text in enumerate((
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger")):
button = tk.Button(root, text=text)
button.grid(row=row, column=0, sticky="ew")
root.mainloop()
Using pack:
import tkinter as tk
root = tk.Tk()
for text in (
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger"):
button = tk.Button(root, text=text)
button.pack(side="top", fill="x")
root.mainloop()

Bryan Oakley
- 370,779
- 53
- 539
- 685
-
2This approach makes use of the fact that the widget that contains the buttons will have the same width of the button of largest width. And expands every button to the width the container has. – Nae Dec 18 '17 at 11:59
5
You can also use width
option while defining your buttons like this:
from tkinter import *
root = Tk()
button = Button(root, text = "Test", width = 5)
button.grid()
root.mainloop()

luciferchase
- 559
- 1
- 10
- 24