- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to stop Tkinter Frame from shrinking to fit its contents?
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.
Example
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()
Output
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.
- Related Articles
- How to fit Tkinter listbox to contents?
- How to stop Tkinter Message widget from resizing?
- How to stop Tkinter after function?
- How to Automatically Resize the Textbox to Fit the Contents in Excel?
- How to stop a loop with a stop button in Tkinter?
- How to Delete Tkinter Text Box's Contents?
- How to Change Tkinter Frame Title?
- How to clear the contents of a Tkinter Text widget?
- How to save the contents of a Textbox in Tkinter?
- How to gray out (disable) a Tkinter Frame?
- How to stop myself from bullying others?
- How to stop copy, paste, and backspace in text widget in tkinter?
- How to clear out a frame in the Tkinter?
- How to Auto-Size a Comment Box to Fit Its Content in Excel?
- How to stop a function during its execution in JavaScript?
