I am making a program that can launch files and programs like a Stream Deck. After selecting the file I want to assign to a button, the button resizes due to the filename being wider that the placeholder text "Add".
I couldn't find any solutions to this problem anywhere.
I am desperate on finding the solution as this is pretty much the final thing i need to fix to make the program pre-Alpha.
Thank you in advace. How to reproduce this issue:
import tkinter
from tkinter import *
root = Tk()
button1 = Button(root, text="Add", padx=10, pady=10)
button2 = Button(root, text="More Text", padx=10, pady=10)
button1.grid(row=0, column=0)
button2.grid(row=1, column=0)
root.mainloop()
CodePudding user response:
A widget appears in its natural size, if not defined otherwise. The natural size is usally the smallest amount of pixels needed to display the widget in width and height. You can change that appearance by defining a width for your Button, even better have a constant if you use the same value for a couple of Buttons. Example:
import tkinter as tk
#from tkinter import *
#dont't use wildcard imports
#only import your modul once
BUTTONWIDTH = 10 #Constant
root = tk.Tk()
button1 = tk.Button(root, text="Add", width=BUTTONWIDTH) #use width
button2 = tk.Button(root, text="More Text", width=BUTTONWIDTH)
button1.grid(row=0, column=0)
button2.grid(row=1, column=0)
root.mainloop()