1

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 than 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()

enter image description here

  • 1
    Please refer to this guide on how to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and read about [how to ask](https://stackoverflow.com/help/how-to-ask). Remember, we can't help you if we don't know what you've already tried. – JRiggles Oct 20 '22 at 12:20
  • Please provide some example code. We need to see what you have done so far to even attempt to assist. – Mike - SMT Oct 20 '22 at 12:29
  • There are three layout engines in tkinter - and there is always the option of truncating the text on your code, before setting it as the button label. Without seeing your code, it is not possible to help, just to write a fresh new program that would work, but that would be so different from your that possibly you could not even adapt there. – jsbueno Oct 20 '22 at 12:40

1 Answers1

1

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()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54