Prevent a tkinter window from shrinking while allowing it to expand?

Viewed 54

I have a tkinter frame where I add a label after getting user-input. I have the command frame.pack_propagate(0) that prevents the frame from shrinking AND expanding. However, I want the frame to be able to expand if the contents of the label added exceeds the locked frame.

Is there a way to prevent a frame from shrinking while allowing it to expand to fit its contents? (i.e minimum frame size with no maximum)

Code

from tkinter import *

FRAME_WIDTH = 300
FRAME_HEIGHT = 300

tk = Tk()
tk.geometry('{}x{}'.format(FRAME_WIDTH, FRAME_HEIGHT))

my_frame = Frame(tk, width=FRAME_WIDTH, height=50, relief='ridge', borderwidth=3)
my_frame.pack()
my_frame.pack_propagate(0)

user_input = '' # in my program text will be here
input_label = Label(my_frame, text=f'You wrote: {user_input}')
input_label.pack(anchor=NW)

tk.mainloop()

This would result in: [tkinter window not shrunk1

Without frame.pack_propagate(0): tkinter window shrunk to fit label

I locked the size of the frame with pack_propagate() because I don't want the frame to fit exactly around the label (seen in 2nd image). However, the text displayed may exceed the given space, so I want a way to have the frame expand while not letting it shrink smaller than it currently is.

0 Answers
Related