Tkinter library in Python
Tags: python, tkinter
Published: 2024-03-22 14:00:00

In this article, we'll dive into the world of **Graphical User Interface (GUI)** development using Python's **Tkinter** library. Tkinter stands as a versatile and user-friendly tool for beginners and advanced programmers alike, simplifying the creation of **GUI applications**. We aim to demystify the process of working with Tkinter, from understanding its core widgets to leveraging its strengths and navigating its limitations. By the end, you'll be equipped with the essentials of Tkinter to enhance your Python GUI development projects.

# Understanding Tkinter

Tkinter, short for **Tk interface**, is a built-in Python library designed for creating interactive GUI applications. Tkinter, built on the Tk GUI toolkit, streamlines the creation of windows, buttons, menus, and other graphical elements. As an integral part of the Python standard library, Tkinter smoothly integrates with Python projects, requiring no extra installations, for a seamless GUI development experience. The official python tkinter documentation offers in-depth explanations of Tkinter modules, classes, and methods. [Visit Documentation][1]

<div class="div-green"> <span class="alert-header">Tip:</span> <span class="alert-body">As you embark on your GUI development journey, start by sketching out the design and functionality of your application before jumping into the code. This can save time and streamline your development process.</span> </div>

# Exploring Basic Widgets

We'll begin by exploring some fundamental components you can construct using Tkinter. The following snippets must be preceeded by:

    import tkinter as tk
    window = tk.Tk()
    window.geometry("457x335")

and followed by:

    window.mainloop()

<div class="div-red"> <span class="alert-header">Caution:</span> <span class="alert-body">When working with widgets, remember to assign a unique name to each widget variable to avoid confusion, especially when your application starts to grow.</span> </div>

## **Label**

Presents either text or an image, lacking interactive features for users. For more information on [label][2] refer official documentation of python.

    label = tk.Label(window, text="This is a Label")
    label.pack()

![Label][3]

## **Button**

An interactive component that executes a specific operation upon being clicked, like dispatching form data or activating a particular function.

    def button_click():
        print("Button clicked")
    
    button = tk.Button(window, text="Click here!", command=button_click)
    button.pack()

![Button][4]

After clicking the button:

![ButtonClicked][5]

## **Entry**

A text field where users can input text or numbers. This component is frequently utilized for entering information or executing search operations.

    entry = tk.Entry(window)
    entry.pack()

  

![Entry][6]

  

## **Text**

  

A text field that provides a multi-line text editing area for users to input or display text.

  

    text = tk.Text(window, height=5, width=30)
    text.pack()

  

![Text][7]

  

## **Scale**

  

Enables users to choose a value from a spectrum by moving a slider control. It is useful for selecting values like volume, brightness, etc.

  

    def update_value(value):
        label.config(text=f"Volume Level: {value}")
    
    scale = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL, command=update_value) #create a scale to select values from 0 to 100
    
    scale.pack()
    label = tk.Label(window, text="Volume Level: ")
    label.pack()

![Scale][8]

  

## **Spinbox**

  

A numerical input field with up and down arrow buttons for incrementing or decrementing the value.

  

    spinbox = tk.Spinbox(window, from_=0, to=10)
    spinbox.pack()

  

![Spinbox][9]

  

## **Separator**

  

A horizontal or vertical line to visually separate sections or groups of widgets.

  

    from tkinter import ttk #for themed widgets
    
    label_1 = tk.Label(window, text="This is above the separator.")
    label_1.pack()
    
    separator = ttk.Separator(window, orient=tk.HORIZONTAL)
    separator.pack(fill=tk.X)
    
    label_2 = tk.Label(window, text="This is below the separator.")
    label_2.pack()

  

![Separator][10]

  

## **Checkbutton**

  

A checkbox enabling users to toggle between selecting and deselecting an option.

  

    check_var = tk.BooleanVar() #To keep track of the checkbox's state (checked or unchecked)
    check_button = tk.Checkbutton(window, text="Checkbox", variable=check_var)
    check_button.pack()

  

![Checkbutton][11]

  

## **Radiobutton**

  

A group of radio buttons, ensuring that users can select only one option from a list of choices.

  

    radio_var = tk.StringVar() #To track which radio button is selected
    radio_1 = tk.Radiobutton(window, text="Option 1", variable=radio_var, value="Option 1")
    radio_2 = tk.Radiobutton(window, text="Option 2", variable=radio_var, value="Option 2")
    radio_1.pack()
    radio_2.pack()

  

![Radiobutton][12]

  

## **Listbox**

  

A widget presenting a list of items, allowing users to select one or more options from the list.

  

## **Scrollbar**

  

Permits users to navigate through content surpassing the widget's visible boundaries, like in a text field or list.

  

    listbox = tk.Listbox(window)
    scrollbar = tk.Scrollbar(window)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y) #scrollbar will be at the right side of the window and expands vertically
    listbox.pack(side=tk.LEFT, fill=tk.BOTH) #listbox will be at the left side of the window and expands horizontally and vertically
    listbox.config(yscrollcommand=scrollbar.set) #Attach scrollbar to listbox
    scrollbar.config(command=listbox.yview)
    
    for i in range(30):
        listbox.insert(tk.END, f"Item {i+1}")

  

![Listbox][13]

  

![Scrollbar][14]

  

## **ColorChooser**

  

A dialog for users to select a color. It provides a graphical interface for choosing colors using a palette or entering RGB values.

  

from tkinter import colorchooser

    def choose_color():
        color = colorchooser.askcolor()
        print("Selected color:", color)
    
    button = tk.Button(window, text="Choose Color", command=choose_color)
    button.pack()

  

![ColorChooser][15]

  

## **Frame**

  

A container used to group and organize other widgets. It is frequently used for layout purposes, creating sections or divisions within a window.

  

    frame = tk.Frame(window, bg="lightblue", bd=5)
    frame.pack()
    
    label = tk.Label(frame, text="Inside the Frame", bg="lightblue")
    label.pack()

  

![Frame][16]

  

## **Canvas**

  

Provides a drawing area for creating graphics, shapes, and custom elements. It is used for creating diagrams, charts, and interactive visualizations.

  

    canvas = tk.Canvas(window, width=200, height=100, bg="white")
    canvas.pack()
    canvas.create_rectangle(50, 25, 150, 75, fill="red") #draw a red-coloured rectangle

  

![Canvas][17]

  

## **PanedWindow**

  

A container for arranging multiple panes or frames with adjustable sizes. It allows users to resize panes by dragging the divider between them.

  

    paned_window = tk.PanedWindow(window, orient=tk.HORIZONTAL)
    paned_window.pack(fill=tk.BOTH, expand=True) #will expand both horizontally and vertically
    frame1 = tk.Frame(paned_window, width=228, height=200, background="red")
    frame2 = tk.Frame(paned_window, width=228, height=200, background="blue")
    paned_window.add(frame1)
    paned_window.add(frame2)

  

![PanedWindow1][18]

  

After dragging the divider to the left:

  

![PanedWindow2][19]

  

## **Menu**

  

Creates a menu bar or dropdown menu with options and commands. It is commonly used to include navigation, settings and additional functionalities into the application's interface.

  

    def print_something():
        print("You chose the first option")
    
    menu_bar = tk.Menu(window)
    
    file_menu = tk.Menu(menu_bar, tearoff=0)
    file_menu.add_command(label="Print Something", command=print_something)
    file_menu.add_separator()
    file_menu.add_command(label="Exit", command=window.quit)
    menu_bar.add_cascade(label="Click to see options", menu=file_menu)
    window.config(menu=menu_bar)

  

![Menu][20]

  

After selecting "Print Something":

  

![MenuOutput][21]

  

These elements serve as fundamental components for Tkinter GUI applications, frequently employed across diverse projects to develop intuitive interfaces.

  

To delve deeper into Tkinter and explore its capabilities, you can start with the following sections of the Python documentation:

  

- **Tkinter — Python interface to Tcl/Tk**: This section provides the foundational knowledge required to create GUI applications with Tkinter, including widget classes, event handling, and more [Python Tkinter Documentation][22].

  

- **Graphical User Interfaces with Tk**: This part of the documentation discusses the integration of Tk/Tcl with Python, offering insights into the architecture of Tkinter applications, the Tkinter module, and thematic widget extensions [Python GUI with Tk][23].

  

- **Tkinter.ttk — Tk themed widgets**: For those interested in using themed widgets for a more modern GUI look, this section details the thematic widget set provided by Tk 8.5 and integrated into Tkinter [Themed Widgets][24].

  

# Conclusion

  

**Tkinter** is a versatile and user-friendly toolkit for **Python GUI development**, offering a **rich set of widgets**, **cross-platform compatibility**, and **seamless integration** with Python code. Despite its limitations, Tkinter remains a popular choice for developers looking for efficient creation of simple to moderately complex GUI applications.

  

  


  [1]: https://docs.python.org/3/library/tkinter.html
  [2]: https://docs.python.org/3/library/tkinter.html#label-widget
  [3]: https://logiclair.org/?qa=blob&qa_blobid=15447304320421249354
  [4]: https://logiclair.org/?qa=blob&qa_blobid=17225166072514634203
  [5]: https://logiclair.org/?qa=blob&qa_blobid=13782699684249808265
  [6]: https://logiclair.org/?qa=blob&qa_blobid=17774851110350440973
  [7]: https://logiclair.org/?qa=blob&qa_blobid=8046130723076572707
  [8]: https://logiclair.org/?qa=blob&qa_blobid=18163681093508975768
  [9]: https://logiclair.org/?qa=blob&qa_blobid=6830706622157727616
  [10]: https://logiclair.org/?qa=blob&qa_blobid=16298979966054953561
  [11]: https://logiclair.org/?qa=blob&qa_blobid=9114035529065436677
  [12]: https://logiclair.org/?qa=blob&qa_blobid=2612228386099183090
  [13]: https://logiclair.org/?qa=blob&qa_blobid=13063392361028412068
  [14]: https://logiclair.org/?qa=blob&qa_blobid=489762057271802018
  [15]: https://logiclair.org/?qa=blob&qa_blobid=8330473602676781386
  [16]: https://logiclair.org/?qa=blob&qa_blobid=9882681441102070094
  [17]: https://logiclair.org/?qa=blob&qa_blobid=3077622398634848951
  [18]: https://logiclair.org/?qa=blob&qa_blobid=11535792397148202103
  [19]: https://logiclair.org/?qa=blob&qa_blobid=11391496175646540405
  [20]: https://logiclair.org/?qa=blob&qa_blobid=1591595257121153453
  [21]: https://logiclair.org/?qa=blob&qa_blobid=5942132127197068255
  [22]: https://docs.python.org/3/library/tkinter.html
  [23]: https://docs.python.org/3/library/tk.html
  [24]: https://docs.python.org/3/library/tkinter.ttk.html