How to set the min and max height or width of a Frame in Tkinter?


In order to manage too many widgets in the window, we can use Frame widgets in Tkinter. In order to place the widgets inside the frame widget, we have to set the relative width and height of the frame. To configure the relative height and width of the frame, we have to configure it using the place(relx, rely) method.

Now, let us create a frame and add a button to it.

Example

#import required libraries
from tkinter import *
from tkinter import ttk

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

#Set the geometry of the window
win.geometry("750x250")

#Create a LabelFrame
frame= Frame(win, width= 100, height= 100, background= "green")
frame.pack(fill= BOTH, expand= True)

#Configure the Frame
frame.place(relx=0.3, rely=.3, anchor= "c")

#Create Button widget in Frame
ttk.Button(frame, text= "Button").pack()

win.mainloop()

Output

Running the above code will display a frame that has a minimum and maximum width/height is set.

Updated on: 04-May-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements