Signup/Sign In

Python Tkinter Frame Widget

The Tkinter Frame widget is used to group and organize the widgets in a better and friendly way. The Frame widget is basically a container (an invisible container) whose task is to hold other widgets and arrange them with respect to each other. The Tkinter frame widget makes up a rectangular region on the screen.

It basically acts as a foundation class which then implements complex widgets. It is like the HTML div tag, which is just to define divisions on a webpage in which we have other HTML elements.

Tkinter Frame Widget

The syntax of the frame widget is given below:

W = Frame(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the look of the frame and these options are written as comma-separated key-value pairs.

Tkinter Frame Widget Options:

Following are the various options used with frame widgets:

Name of the option Description
bd This option is used to represent the width of the border. Its default value is 2 pixels.
bg This option is used to indicate the normal background color of a widget.
cursor With the help of this option, the mouse pointer can be changed to the cursor type which is set to different values like an arrow, dot, etc.
height This option is used to indicate the height of the frame.
width This option is used to indicate the width of the frame.
highlightbackground This option denotes the color of the background color when it is under focus.
highlightthickness This option is used to specify the thickness around the border when the widget is under the focus.
relief This option specifies the type of the border of the frame. Its default value is FLAT
highlightcolor This option is mainly used to represent the color of the focus highlight when the frame has the focus.

Tkinter Frame Widget Example

Below we have a basic example where we will organize different button widgets in a Frame widget. Let us see the code snippet given below:

from tkinter import *
 
root = Tk() 
root.geometry("300x150") 

w = Label(root, text ='StudyTonight', font = "80") 
w.pack() 

frame = Frame(root) 
frame.pack() 

bottomframe = Frame(root) 
bottomframe.pack(side = BOTTOM) 

button1 = Button(frame, text ="Block1", fg ="red") 
button1.pack(side = LEFT) 

button2 = Button(frame, text ="Block2", fg ="brown") 
button2.pack(side = LEFT) 

button3 = Button(frame, text ="Block3", fg ="blue") 
button3.pack(side = LEFT) 

button4 = Button(bottomframe, text ="Block4", fg ="orange") 
button4.pack(side = BOTTOM) 

button5 = Button(bottomframe, text ="Block5", fg ="orange") 
button5.pack(side = BOTTOM) 

button6 = Button(bottomframe, text ="Block6", fg ="orange") 
button6.pack(side = BOTTOM) 

root.mainloop()

In the code example above, we have created two frame widgets and then we have added 3 each button in those frames and have use Tkinter geometry manager to arrange the buttons inside the application window,

Summary

In this tutorial, we learned about Tkinter Frame widget and how we can use it to manage other Tkinter widgets and position them properly.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight