SlavaCat
SlavaCat

Reputation: 91

Formatting with 'sticky' stopped working?

I have no idea why, I've copied code in from other programs that works perfectly fine but just doesn't here. I have this code:

from tkinter import *
from tkinter import ttk

#Create Window
main = Tk()
main.geometry('200x200')

Button(main, text = 'WHY WONT STICKY WORK') .grid(row = 0, column = 0, sticky = NSEW)

main.mainloop()

And it produces this result: help

Please someone tell me that I'm just missing something painfully obvious, thanks!

Upvotes: 0

Views: 366

Answers (1)

Martin Wettstein
Martin Wettstein

Reputation: 2894

It actually does work. But your column 0 is just not that wide. It's sticky to the E and W border of that column but to the right, there is column 1.

You defined the width of your widget to be 200px. But the width of the button is just about 170px. So, tkinter must assume, that the remaining space is used for a different column. If you insert a button beneath that which has more text, you will see that the first one gets wider (e.g. Button(main, text = 'WHY WONT STICKY WORK AGAIN').grid(row = 1, column = 0, sticky = N+S+E+W)).

If you want column 0 to span your complete window, you have to define this column as the one that gets spread out if the window size changes. Just add the line

main.columnconfigure(0, weight=1)

after the definition of the window geometry. This means that column 0 will be expanded when the width of your window changes. So, column 0 will span the whole 200px and your button will reach from one end of the window to the other.

Upvotes: 4

Related Questions