How to change the width of a Frame dynamically in Tkinter?


The frame widget in Tkinter works like a container where we can place widgets and all the other GUI components. To change the frame width dynamically, we can use the configure() method and define the width property in it.

Example

In this example, we have created a button that is packed inside the main window and whenever we click the button, it will update the width of the frame.

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

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

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

def update_width():
   frame.config(width=100)

# Create a frame
frame=Frame(win, bg="skyblue3", width=700, height=250)
frame.pack()

# Add a button in the main window
ttk.Button(win, text="Update", command=update_width).pack()

win.mainloop()

Output

Run the above code to display a window that contains a frame widget and a button.

Click the "Update" button to update the width of the frame.

Updated on: 05-Aug-2021

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements