Open In App

What are Widgets in Tkinter?

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

Tkinter is Python’s standard GUI (Graphical User Interface) package. tkinter we can use to build out interfaces – such as buttons, menus, interfaces, and various kind of entry fields and display areas. We call these elements Widgets.

What are Widgets in Tkinter?

In Tkinter, a widget is essentially a graphical component that the user can interact with. They can range from simple elements like buttons and labels to more complex ones like text entry fields, listboxes, and canvases. Each widget serves a specific purpose and can be customized to fit the design and functionality requirements of your application.

How Do Tkinter Widgets Work?

Each widget in Tkinter is an instance of a specific class defined in the Tkinter Module. These classes provide methods and attributes that allow you to configure the widget’s appearance, behavior, and functionality. Widgets are typically added to the application’s window or frames using layout managers like pack(), grid(), or place(), which determine their position and size within the interface.

Example: 

Python
from tkinter import *

# create root window
root = Tk()

# frame inside root window
frame = Frame(root)

# geometry method
frame.pack()

# button inside frame which is
# inside root
button = Button(frame, text='Geek')
button.pack()

# Tkinter event loop
root.mainloop()

Output : 

Tkinter Widget Basics

Tkinter supports the below mentioned core widgets – 

LabelDisplay static text or images.
ButtonIt is used to add buttons to your application
EntryIt is used to input single line text entry from user
FrameIt is used as container to hold and organize the widgets
RadioButtonIt is used to implement one-of-many selection as it allows only one option to be selected
CheckbuttonCreate checkboxes for boolean options.
ListBoxDisplay a list of items for selection.
ScrollbarAdd scrollbars to widgets like Listbox.
MenuIt is used to create all kinds of menu used by an application
CanvasDraw shapes, lines, text, and images.

Tkinter Widget Intermidate

WidgetsDescription
TextCreate a multiline text input with advanced editing capabilities.
ComboBoxProvide a dropdown list with editable text entry.
ScaleCreate a scale widget for selecting values within a range.
TopLevelCreate additional windows/dialogs.
MessageDisplay simple messages or notifications.
MenuButtonCreate a button that opens a menu.
ProgressBarShow progress of a task.
SpinBoxProvide a numerical input with up/down arrows.

Tkinter Widget Advance

WidgetsDescription
ScrolledTextCreate a text widget with built-in scrollbars.
TreeviewDisplay hierarchical data in a tree-like structure.
MessageBoxDisplay dialog boxes for messages, warnings, etc.
Treeview ScrollbarAdd scrollbars to Treeview widgets.

Geometry Management

Creating a new widget doesn’t mean that it will appear on the screen. To display it, we need to call a special method: either grid, pack(example above), or place

MethodDescription
pack()The Pack geometry manager packs widgets in rows or columns.
grid()The Grid geometry manager puts the widgets in a 2-dimensional table. 
The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget.
place()The Place geometry manager is the simplest of the three general geometry managers provided in Tkinter. 
It allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window.

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads