Open In App

Python Tkinter – Frame Widget

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.

Note: For more reference, you can read our article, 

  1. What is Widgets
  2. Python Tkinter Overview
  3. Python Tkinter Tutorial

What is Frame in Tkinter?

A frame is a rectangular region on the screen. A frame can also be used as a foundation class to implement complex widgets. It is used to organize a group of widgets.

Tkinter Frame Widget Syntax

The syntax to use the frame widget is given below.

Syntax: w = frame( master, options)

Parameters:

  • master: This parameter is used to represent the parent window.
  • options: There are many options which are available and they can be used as key-value pairs separated by commas.

Tkinter Frame Options

The following are commonly used Options that can be used with this widget:

  • bg: This option used to represent the normal background color displayed behind the label and indicator.
  • bd: This option used to represent the size of the border around the indicator and the default value is 2 pixels.
  • cursor: By using this option, the mouse cursor will change to that pattern when it is over the frame.
  • height: The vertical dimension of the new frame.
  • highlightcolor: This option used to represent the color of the focus highlight when the frame has the focus.
  • highlightthickness: This option used to represent the color of the focus highlight when the frame does not have focus.
  • highlightbackground: This option used to represent the thickness of the focus highlight..
  • relief: The type of the border of the frame. It’s default value is set to FLAT.
  • width: This option used to represents the width of the frame.

Frame Widget in Tkinter Example

In this example, below code defines functions to create Tkinter widgets with customized options, facilitating widget creation with fewer lines of code. It generates a GUI with a main window containing a frame, labels, and buttons, all with specified options like background color, border size, cursor style, and more, enhancing visual consistency and usability.

Python3
import tkinter as tk

# Function to create widgets with all options
def create_widget(parent, widget_type, **options):
    return widget_type(parent, **options)

# Create the main window
window = create_widget(None, tk.Tk)
window.title("GUI Example")

# Create a Frame widget with all options
frame = create_widget(window, tk.Frame, bg='lightblue', bd=3, cursor='hand2', height=100, 
                      highlightcolor='red', highlightthickness=2, highlightbackground='black', 
                      relief=tk.RAISED, width=200)
frame.pack(padx=20, pady=20)

# Create Label widget with all options
label = create_widget(frame, tk.Label, text='GeeksForGeeks', font='50', bg='lightblue', bd=3, cursor='hand2',
                      highlightcolor='red', highlightthickness=2, highlightbackground='black', 
                      relief=tk.RAISED)
label.pack()

# Create a frame for buttons
button_frame = create_widget(window, tk.Frame, bg='lightblue', bd=3, cursor='hand2', height=50, 
                              highlightcolor='red', highlightthickness=2, highlightbackground='black', 
                              relief=tk.RAISED, width=200)
button_frame.pack(pady=10)

# Function to create buttons with all options
def create_button(parent, text, fg):
    return create_widget(parent, tk.Button, text=text, fg=fg, bg='lightblue', bd=3, cursor='hand2',
                         highlightcolor='red', highlightthickness=2, highlightbackground='black', 
                         relief=tk.RAISED)

# Create buttons
buttons_info = [("Geeks1", "red"), ("Geeks2", "brown"), ("Geeks3", "blue"), 
                ("Geeks4", "green"), ("Geeks5", "green"), ("Geeks6", "green")]

for text, fg in buttons_info:
    button = create_button(button_frame, text=text, fg=fg)
    button.pack(side=tk.LEFT)

# Run the Tkinter event loop
window.mainloop()

Output

Tkinter Frame Widget

Python Tkinter – Frame Widget




Previous Article
Next Article

Similar Reads

How to create a frame inside a frame tkinter?
Prerequisite: Tkinter It is very easy to create a basic frame using Tkinter, this article focuses on how another frame can be created within it. To create a basic frame the name of the parent window is given as the first parameter to the frame() function. Therefore, to add another frame within this frame, just the name of the first frame needs to g
2 min read
How to make a proper double scrollbar frame in Tkinter
Tkinter is a Python binding to the Tk GUI(Graphical User Interface) Toolkit. It is a thin-object oriented layer on top of Tcl/Tk. When combined with Python, it helps create fast and efficient GUI applications. Note: For more information refer, Python GUI-tkinter Steps to Create a double scrollbar frame in Tkinter 1) Firstly, the module Tkinter will
3 min read
How To Place An Image Into A Frame In Tkinter?
In this article, we will walk through the process of placing an image into a frame in Tkinter. We will cover everything from setting up your environment to loading and displaying images using the PIL (Python Imaging Library) module, specifically Pillow. By the end of this tutorial, you will have a solid understanding of how to manage and display im
5 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Progressbar widget in Tkinter | Python
The purpose of this widget is to reassure the user that something is happening. It can operate in one of two modes - In determinate mode, the widget shows an indicator that moves from beginning to end under program control. In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indica
2 min read
Python | PanedWindow Widget in Tkinter
Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The PanedWindow widget is a geometry manager widget, which can contain one or more child widgets panes. The child widgets can be resized by the user, by moving separator lines sashes using the mouse. Syntax: PanedWindow(master, **options) Parameters: master:
3 min read
Python Tkinter - Toplevel Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Toplevel widget A Toplevel widget is used to create a window on top of all other w
3 min read
Combobox Widget in tkinter | Python
Python provides a variety of GUI (Graphic User Interface) types such as PyQT, Tkinter, Kivy, WxPython, and PySide. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to understand. The word Tkinter comes from the Tk interface. The tkinter module is available in Python standard library which has to be impo
3 min read
Python Tkinter - Entry Widget
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.In Python3 Tkinter is come preinstalled But you can also install it by using
3 min read
Python Tkinter - ListBox Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter ListBox widget The ListBox widget is used to display different types of items. The
2 min read
Practice Tags :