Python 3 – Tkinter Frame
In this tutorial, we will explore Tkinter Frame in detail. Tkinter is the standard GUI toolkit for Python. It is one of the easiest and simplest ways to create graphical user interfaces in Python.
Tkinter is included with Python3 and should be installed by default. But, if it is not installed, you can easily install it using pip
.
pip install tkinter
What is Tkinter Frame?
Tkinter Frame is a rectangular area that can contain other widgets. It acts as a container for other widgets like Buttons, Labels, Textboxes, etc.
A Frame widget is created by calling the Frame()
constructor.
frame = Frame(master, **options)
master
is the parent widget. It is the widget that contains the Frame widget. The options
parameter is an optional dictionary of configuration options.
Creating a Tkinter Frame
Let’s create a simple Tkinter Frame with a Label inside it.
from tkinter import *
#Create the main window
root = Tk()
#Create the Frame widget
frame = Frame(root, bg="red")
#Create the Label widget
lbl = Label(frame, text="Hello, World!", bg="yellow", font=("Arial Bold", 20))
#Pack the Label widget
lbl.pack()
#Pack the Frame widget
frame.pack(side=LEFT, padx=10, pady=10)
#Start the GUI event loop
root.mainloop()
This will create a Frame and a Label inside it. The Frame is colored red and the Label is colored yellow. The Frame has a 10-pixel padding on both sides.
Frame Options
The Frame widget has various options that can be set to customize its appearance and behavior.
bg
The background color of the Frame. You can set it using the bg
option.
frame = Frame(root, bg="red")
width and height
The width and height of the Frame. You can set them using the width
and height
options.
frame = Frame(root, width=200, height=100)
padx and pady
The horizontal and vertical padding of the Frame. You can set them using the padx
and pady
options.
frame = Frame(root, padx=10, pady=10)
relief
The border style of the Frame. You can set it using the relief
option.
frame = Frame(root, relief=RAISED)
borderwidth
The width of the border of the Frame. You can set it using the borderwidth
option.
frame = Frame(root, borderwidth=5)
Nesting Frames
You can nest multiple Frames inside each other to create complex layouts.
from tkinter import *
#Create the main window
root = Tk()
#Create the Frames
frame1 = Frame(root, bg="red", width=200, height=100)
frame2 = Frame(root, bg="green", width=100, height=50)
frame3 = Frame(frame1, bg="blue", width=50, height=50)
#Pack the inner Frame
frame3.pack(side=LEFT, padx=10, pady=10)
#Pack the middle Frame
frame2.pack(side=LEFT, padx=10, pady=10)
#Pack the outer Frame
frame1.pack(side=LEFT, padx=10, pady=10)
#Start the GUI event loop
root.mainloop()
This will create a red Frame with a blue Frame inside it, and a green Frame next to it.
Conclusion
Tkinter Frame is a powerful and flexible widget that can be used to create complex layouts in your Python applications. It provides a simple and intuitive way to create graphical user interfaces for your Python applications. With its many options and features, you can customize the appearance and behavior of your Frames to suit your needs. So start exploring Tkinter Frame today and create amazing Python applications with ease.