2

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.

Thomas Huitema
  • 133
  • 1
  • 9
  • Please create a [mcve] that illustrates what you're asking about. It's not clear why you don't want it to shrink. There are ways to prevent a window from shrinking below a certain size, but it's hard to know what you're really trying to accomplish. – Bryan Oakley Apr 02 '21 at 21:35
  • I added a minimal reproducible example. – Thomas Huitema Apr 03 '21 at 01:17
  • If you don't want the frame to shrink, have you considered just having the frame fill the window, and then setting a minimum size on the window? – Bryan Oakley Apr 03 '21 at 04:16

0 Answers0