Tkinter Widgets In Python

In this article, we will learn different Tkinter Widgets in Python. Tkinter in Python comes with a lot of good widgets. Widgets are standard graphical user interface (GUI) elements, like different kinds of buttons and menus.

If you are new to Tkinter then you can follow this blog for a better understanding of Tkinter.

Label Widget

Label widget shows text to the user. You can update the widget programmatically to, for example, provide a readout or status bar. A Label widget shows text to the user. You can update the widget programmatically to, for example, provide a readout or status bar.

window_label = Label( master, option, … )

master = This represents the parent window
option = These options can be used as key-value pairs separated by commas. List of most commonly used options for this widget like an anchor, bg, fg, width, height, textvariable, etc.

Button Widget

The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.

window_button = Button ( master, option=value, ... )

option = These options can be used as key-value pairs separated by commas. List of most commonly used options for this widget like an activebackground, activeforeground, command, width, height, image, padx, pady etc.

Entry Widget

The Entry Widget is a Tkinter Widget used to Enter or display a single line of text.

entry = tk.Entry(parent, options)

Parent: The Parent window or frame in which the widget to display.
Options: The various options provided by the entry widget are

  • bg: The normal background color displayed behind the label and indicator
  • fg: The color used to render the text.
  • font: The font used for the text.
  • justify: If the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT.
  • relief: With the default value, relief=FLAT. You may set this option to any of the other styles like SUNKEN, RIGID, RAISED, or GROOVE
  • textvariable: In order to be able to retrieve the current text from your entry widget, you must set this option to an instance of the StringVar class.
import tkinter as tk
# setting the windows size using .geometry
# Declaring string variable for storing name and password
# If you need to store integer value then use tk.IntVar()

name_var = tk.StringVar()
password_var = tk.StringVar()
def submit():
    name=name_var.get()   # .get() function to we can get the value of the variable
    print("The name is : " + name)
    print("The password is : " + password)
    name_var.set("") # .set() function to we can set the value of the variable
name_label = tk.Label(root, text = 'Username', font=('calibre',10, 'bold'))
name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))

password_label = tk.Label(root, text = 'Password', font = ('calibre',10,'bold'))
password_entry = tk.Entry(root, textvariable = password_var, font = ('calibre',10,'normal'), show = '*')
submit_button = tk.Button(root,text = 'Submit', command = submit)
# placing the label and entry in the required position using grid method


Radiobutton Widget

This widget implements a multiple-choice button, which is a way to offer many possible selections to the user and lets the user choose only one of them.

v = tk.IntVar()
radiobutton_widget1 = tk.Radiobutton(root, text="Radiobutton 1", variable=v, value=1)
radiobutton_widget2 = tk.kRadiobutton(root, text="Radiobutton 2", variable=v, value=2)

Checkbutton Widget

Checkbutton records on/off or true/false status. Like a Radiobutton, a Checkbutton widget can be displayed without its checkmark, and you need to use a Tkinter variable to access its state.

checkbutton = tk.Checkbutton(root, text="Checkbutton")

Text Widget

Tkinter Text box widget is used to insert multi-line text. This widget can be used for messaging, displaying information, and many other tasks.

text_widget = tk.Text(root, width=20, height=3)
text_widget.insert(tk.END, "This is TEXT WIDGET")

Menu Widget

The Menu widget can create a menu bar. Creating menus can be hard, especially if you want drop-down menus. To do that, you use a separate Menu widget for each drop-down menu you’re creating.

import tkinter as tk

root = tk.Tk()

def menu_callback():
    print("I'm in the menu callback!")

def submenu_callback():
    print("I'm in the submenu callback!")

menu_widget = tk.Menu(root)
submenu_widget = tk.Menu(menu_widget, tearoff=False)

submenu_widget.add_command(label="Submenu Item1", command=submenu_callback)
submenu_widget.add_command(label="Submenu Item2", command=submenu_callback)

menu_widget.add_cascade(label="Item1", menu=submenu_widget)
menu_widget.add_command(label="Item2", command=menu_callback)
menu_widget.add_command(label="Item3", command=menu_callback)



I hope this article helps you and you will like it.

Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories