1
from tkinter import *
window = Tk()

x = Button(text="a", padx=20)
y = Button(text="aaaaa", padx=20)

x.grid(padx=20)
y.grid(padx=20)


window.mainloop()

Despite having the same padding values, x and y buttons have the same size and are displayed at different distances from the left border of the window. In addition, What unit are the padx values?

Kun.tito
  • 165
  • 1
  • 7
  • When grid takes control over `x`, it expands the column width. After that when you call `grid` on `y` it expands the column width again. Both times it expands it by 20 pixels. You don't actually need the `padx=20` inside the `Button` constructor. – TheLizzard Feb 14 '21 at 17:37
  • `grid` doesn't seem to do that as i removed `padx=20` in the `Button` constructor, it reduced the width of the buttons `x` and `y`. leaving them relatively in the same position with respect to the `window` left border @TheLizzard – Kun.tito Feb 14 '21 at 17:49
  • never mind my last comment I was being stupid :D – TheLizzard Feb 14 '21 at 17:55

1 Answers1

0

Look at this:

The grid(padx=20) adds 20 pixels to the left and right of the button. The padx=20 that is inside the constructor adds 20 pixels to the left and right of the text inside the button. As you aren't allowing the top button to expand, it centres itself in the middle of the window. As that button is smaller (because it has less text inside it) it looks like there is more free space to the left/right of the button

If you allow both buttons to expand like this:

x.grid(padx=20, sticky="news")
y.grid(padx=20, sticky="news")

You will see that the top button becomes the same size as the bottom one.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31