Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tkinter buttons the same size

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?

like image 644
somerandomguy95 Avatar asked Dec 17 '17 22:12

somerandomguy95


Video Answer


2 Answers

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()
like image 59
Bryan Oakley Avatar answered Sep 30 '22 03:09

Bryan Oakley


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()
like image 31
luciferchase Avatar answered Sep 30 '22 04:09

luciferchase