How to explicitly resize frames in tkinter?


The Frames widget in tkinter is generally used to display widgets in the form of a container. The Frame widget works similar to the default window container. The geometry and size of the frame widget can be configured using various geometry managers available in the tkinter library.

Considering the case, if you want to configure the size of the frame explicitly, you can use the pack() geometry manager by specifying the side and padding property. The pack() geometry manager gives proper accessibility to the widget for resizing.

Example

In the following example, we will create two frames and resize them using the pack() geometry manager property.

# Import the required libraries
from tkinter import *

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

# Define the size of the window
win.geometry("700x350")

# Define a function
def exit_win():
   win.destroy()

# Define a frame
button_container=Frame(win, relief="sunken", borderwidth=2)
button_container.pack(side="left", fill="x")

side_container=Frame(win, relief="sunken", borderwidth=2)
side_container.pack(side="left", fill= "y")

# Add widgets in frames
exit_btn=Button(button_container, text="Cancel", command=exit_win)
exit_btn.pack(side="left", padx= 10)
save_btn=Button(button_container, text="Save")
save_btn.pack(side="left", padx=10)

# Add a label widget in side_container frame
txt_label=Label(side_container, text="Tkinter is a Python Library", font=('Helvetica 15 bold'))

txt_label.pack(side= "right", padx=10)

win.mainloop()

Output

Running the above code will display a window with two frames. In each frame, there are text and button widgets. The frame widget can be explicitly resized using the geometry manager.

Updated on: 22-Dec-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements