How to stop Tkinter Frame from shrinking to fit its contents?

TkinterServer Side ProgrammingProgramming

The sizing property of all the widgets in the Tkinter frame is by default set to the size of the widgets themselves. The frame container shrinks or grows automatically to fit the widgets inside it. To stop allowing the frame to shrink or grow the widget, we can use propagate(boolean) method with one of any pack or grid geometry manager. This method stops the frame from propagating the widgets to be resized.

There are two general ways we can define the propagate property,

  • pack_propagate (False) – If the Frame is defined using the Pack Geometry Manager.

  • grid_propagate (False) – If the Frame is defined using the Grid Geometry Manager.


In this example, we will create two entry widget inside a frame which don’t allow the widget to be shrink or fit.

#Import the required Libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

#Create an instance of Tkinter frame
win = Tk()

#Set the geometry of tkinter frame

#Define a function to show a message
def submit():
   messagebox.showinfo("Success", "Submitted Successfully!")

#Creates top frame
frame1 = LabelFrame(win, width= 400, height= 180, bd=5)

#Stop the frame from propagating the widget to be shrink or fit

#Create an Entry widget in Frame1
entry1 = ttk.Entry(frame1, width= 40)
entry1.insert(INSERT,"Enter Your Name")
entry2= ttk.Entry(frame1, width= 40)
entry2.insert(INSERT, "Enter Your Email")

#Creates bottom frame for button
frame2 = LabelFrame(win, width= 150, height=100)

#Create a Button to enable frame
button1 = ttk.Button(frame2, text="Submit", command= submit)



Run the above code to display a window that contains two frames with widgets defined in it.

In the given output, the window contains a frame with entry widget that doesn’t shrink the widgets defined inside it.

Updated on 04-May-2021 14:20:20