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.


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

# Set the size of the window

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'))

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

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



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.