Best post on Frame Widget of tkinter in Python

Frame is Widget of tkinter in Python

Tkinter provides many widgets for GUI in Python.

The Frame widget is a container widget. The frame can have a border and a background, and is used to group other widgets when creating an application or dialog layout.

Frame Widget

Frame widget is responsible for arranging the position of other widgets.
A frame is rectangular region on the screen. This widget is mainly used as a geometry master for other widgets, or to provide padding between other widgets.

This widget can be used for decorating and enhancing look and feel for users.
This widgets are used to group multiple other widgets into complex layouts.
This widget makes the layout user friendly.
With the help of this widget you can divide the screen into multiple frames.

Syntax
Here is the simple syntax to create this widget −

w = Frame( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Various options of Frame widget:

  • bg : The normal background color displayed behind the widget and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • cursor : The cursor to display when the mouse moves over the widget. By default, the standard cursor is used.
  • fg : This option specifies the color of the text.
  • height : The vertical dimension of the new frame.
  • width: Width of the widget in characters (not pixels!). If this option is not set, the widget will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
  • takefocus : If true, the user can navigate to this widget using the Tab key. The default value is 0.
  • highlightbackground : Color of the background when frame does not have focus.
  • highlightthickness : Thickness of focus highlight
  • highlightcolor : This option used to represent the color of the focus highlight when the frame has the focus.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date : 2nd April 2020

Description : Program to show use of Frame widget

"""
from tkinter import *
root=Tk()
frame=Frame(root)
frame.pack()
bottomframe=Frame(root)
bottomframe.pack(side=BOTTOM)
topframe=Frame(root)
topframe.pack(side=TOP)
b1=Button(frame,text="itvoyagers.in in frame",fg="yellow",bg="brown",font="verdana")
b1.pack(side=LEFT)
b2=Button(frame,text="itvoyagers.in in frame",fg="orange",bg="brown",font="verdana")
b2.pack(side=LEFT)
b1=Button(bottomframe,text="itvoyagers.in in bottom frame",fg="sky blue",bg="brown",font="verdana")
b1.pack(side=LEFT)
b2=Button(bottomframe,text="itvoyagers.in in bottom frame",fg="white",bg="brown",font="verdana")
b2.pack(side=LEFT)
b1=Button(topframe,text="itvoyagers.in in top frame",fg="magenta",bg="brown",font="verdana")
b1.pack(side=LEFT)
b2=Button(topframe,text="itvoyagers.in in top frame",fg="cyan",bg="brown",font="verdana")
b2.pack(side=LEFT)

Output of Frame widget (itvoyagers.in)
Output of Frame widget (itvoyagers.in)

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date : 2nd April 2020

Description : Program to show use of Frame widget

"""
from tkinter import *  
root = Tk()

Frame(height = 30,width = 500,bg = 'orange',highlightthickness=2,highlightbackground="blue",relief=SUNKEN,cursor="cross").pack()
Frame(height = 30,width = 500,bg = 'white',highlightthickness=2,highlightbackground="blue",relief=RAISED,cursor="spider").pack()
Frame(height = 30,width = 500,bg = 'green',highlightthickness=2,highlightbackground="blue",relief=FLAT,cursor="heart").pack()
Frame(height = 30,width = 500,bg = 'brown',highlightthickness=2,highlightbackground="blue",relief=GROOVE,cursor="circle").pack()
Frame(height = 30,width = 500,bg = 'yellow',highlightthickness=2,highlightbackground="blue",relief=SOLID,cursor="shuttle").pack()
Frame(height = 30,width = 500,bg = 'red',highlightthickness=2,highlightbackground="blue",relief=RIDGE,cursor="star").pack()
Frame(height = 30,width = 500,bg = 'grey',highlightthickness=2,highlightbackground="blue",relief=SUNKEN,cursor="pirate").pack()
Frame(height = 30,width = 500,bg = 'magenta',highlightthickness=2,highlightbackground="blue",relief=RAISED,cursor="plus").pack()

OUTPUT
Output of Frame widget (itvoyagers.in)
Output of Frame widget (itvoyagers.in)

You can also check following posts to know more about tkinter and GUI

Leave a Comment