Home > database >  Why do pythons tkinter grid buttons stretch when a label in the grid changes size?
Why do pythons tkinter grid buttons stretch when a label in the grid changes size?

Time:12-03

I am making a simple application with 1 button and 1 label where pressing the button changes the text on the label. Both label and button and placed using the tkinter's grid system however when I press the button the label's text changes size as expected but the button becomes stretched to the label's length too. Why? This is my code.

import tkinter as tk
def change():
    label1.config(text = "example text that is really big which shows the button stretching")
window = tk.Tk()
window.config(bg="black")
b1=tk.Button(window,text="button1",font=("Segoe UI",40),command=change).grid(row=1,column=1,sticky="news")
label1=tk.Label(text = "text",font=("Segoe UI",20),bg="black",fg="white")
label1.grid(row=2,column=1,sticky="news")
window.mainloop()

The expected result was the label changing and the button's size staying the same but instead the button becomes stretched to the labels length. I have tried a lot of things trying to make this work but I can't figure it out. enter image description here

  • Related