Using a Frame class in a Tk class in Python tkinter


Tkinter Frame widget is very useful for grouping multiple widgets in a frame. It includes all the functions and properties that applies to the parent window.

To create a Frame widget, we can instantiate an object of the Frame class. Once we define the Frame widget in the window, we can directly pick any widget and place it into the frame.

Example

In this example, we've created a Frame widget and defined some widgets in it.

# 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("700x250")

def on_click():
   label["text"]="Hello "+ str(entry.get())

# Create a Frame widget
frame=Frame(win, width=400, height=300)

# Add a label in the frame widget
label=Label(frame, text="Enter your name", font=('Calibri 13'))
label.pack(pady=10)

# Add an Entry widget
entry=Entry(frame, width=25)
entry.pack()

# Create a button
ttk.Button(frame, text="Click Me", command=on_click).pack()
frame.pack()

win.mainloop()

Output

Running the above code will display a window that will contain an Entry widget, a label widget, and a button in a frame.

Type your name in the given text field and click the button to display the message on the screen.

Updated on: 05-Aug-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements