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 win.geometry("750x250") #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) frame1.pack() #Stop the frame from propagating the widget to be shrink or fit frame1.pack_propagate(False) #Create an Entry widget in Frame1 entry1 = ttk.Entry(frame1, width= 40) entry1.insert(INSERT,"Enter Your Name") entry1.pack(pady=30) entry2= ttk.Entry(frame1, width= 40) entry2.insert(INSERT, "Enter Your Email") entry2.pack() #Creates bottom frame for button frame2 = LabelFrame(win, width= 150, height=100) frame2.pack() #Create a Button to enable frame button1 = ttk.Button(frame2, text="Submit", command= submit) button1.pack() win.mainloop()
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.