• Articles
  • Tutorials
  • Interview Questions

What is tkinter Module in Python? Architecture, Module, and Example

What is tkinter Module in Python? Architecture, Module, and Example

In this blog, we have covered everything you need to know about the tkinter module for building a graphical user interface for your application. We will be learning about all the major widgets of tkinter. Further, you will also learn the advantages of the tkinter module in Python.

Table of Contents

Check out this YouTube video specially designed for Python beginners:

Video Thumbnail

What is the tkinter Module in Python?

The tkinter module in Python is the standard GUI (Graphical User Interface) toolkit for creating and developing graphical applications. It provides a set of tools and widgets to build user interfaces. With the help of tkinter, developers can build window applications with buttons, menus, text boxes, and other interactive elements. 

Learn more about Python from this Python Data Science Course to get ahead in your career!

How to Install tkinter

Although tkinter is included with standard Python installations, there’s usually no need for a separate installation process. However, if you find that tkinter is not available or installed on your system, you might need to install it manually. Here’s how you can install tkinter based on your operating system:

  • For Windows:

While installing Python, tkinter is usually included by default with Python on Windows. Generally, there is no separate installation required. If it is not installed, then you can install tkinter by running the following command in the command prompt (cmd) : 

pip install tk

  • For macOS:

tkinter is typically pre-installed with Python on macOS. If you encounter issues or need to install it separately, consider using a package manager like Homebrew. You can install Python with tkinter using Homebrew by running the following command in the terminal:

brew install python-tk

  • For Linux:

tkinter might not be included with some Python distributions on Linux. You can install it using the package manager specific to your distribution. For example, Debian/Ubuntu:

sudo apt-get install python3-tk

Learn the basics of Python from Python Fundamentals tutorial.

How to Build Python GUI Application with tkinter

Here we have given a  code that will create a basic window using tkinter with a specified title and size. You can further enhance this window by adding buttons, labels, text boxes, and other widgets to create a fully functional graphical user interface in Python using tkinter. Let’s understand step by step how you can create a window using tkinter: 

Step 1: Run the following command to import the tkinter module: 

import tkinter as tk

Step 2: This command will create the main window or the root window.

root = tk.Tk()

Step 3: This command will set the title of the window to “Intellipaat App”.

root.geometry("400x300")

Step 4: Sets the initial size of the window to a width of 400 pixels and a height of 300 pixels.

root.geometry("400x300")

Step 5: Finally, we will be displaying the window with the help of the following command. This command will give the user the command to enter the main event loop of tkinter, which listens for events such as button clicks, mouse movements, etc., and keeps the window displayed until it’s closed by the user:

root.mainloop()

Output:

tkinter window application

In the above image, as you can see, we have got an application window with the name as “Intellipaat App” and a size of  400 x 300.

Want to know about the real-world uses of Python? Read our detailed blog on Python Applications now.

Widgets of tkinter Module in Python

tkinter module of Python offers a wide range of widgets. By using these widgets, we can add various features to your application. Let’s learn the functionality of some of the most popular widgets in the tkinter module in Python : 

  • Label: Displays text or images. It supports various fonts, text alignments, background colors, and images.
tk.Label(root, text="Hello, tkinter!")
  • Button: Triggers an action when clicked. It supports text or image display, command execution on click, and customizations like colors, fonts, and sizes.
 tk.Button(root, text="Click me!", command=callback_function)
  • Entry: Single-line text input field. Accepts user input, allows text entry, and supports password input using the `show` attribute.
tk.Entry(root)
  • Text: Multi-line text input/output area. Supports text editing, formatting, insertion of images, tags for different styling, and scrolling.
tk.Text(root, height=5, width=30)
  • Frame: Container for organizing and grouping widgets. It helps organize widgets and supports background color, border, and padding settings.
tk.Frame(root)
  • Canvas: Drawing area for graphical elements. It helps draw shapes, lines, text, images, and custom graphics.
tk.Canvas(root, width=200, height=100) 
  • Check button: Represents a binary choice (on/off). It supports checkmark states, text or image display, and variable association for data handling.
tk.Checkbutton(root, text="Enable", variable=check_var)
  • Radio button: Presents a set of options from which only one can be selected. It creates a set of mutually exclusive options, supports text or image display, and allows variable association.
tk.Radiobutton(root, text="Option 1", variable=radio_var, value=1)
  • Listbox: Displays a list of items for selection. It shows a scrollable list, supports multiple selections, and allows insertion, deletion, and manipulation of list items.
tk.Listbox(root)
  • Scrollbar: It enables scrolling for widgets that exceed the viewable area. Associated with widgets like Text, Canvas, and Listbox for scrolling content that doesn’t fit the window.
tk.Scrollbar(root, orient="vertical", command=my_widget.yview)
  • Menu: Creates dropdown menus. It supports hierarchical menu structures, cascading submenus, and associating actions with menu items.
tk.Menu(root)
  • MessageBox: Displays various types of message boxes (e.g., alert, warning, information). Presents popup message boxes for displaying information, warnings, errors, or asking for user input.
tk.messagebox.showinfo("Title", "Message")
  • Scale: Slider for selecting values from a range. It allows the selection of values within a specified range, supports various orientations, and provides a customizable appearance.
tk.Scale(root, from_=0, to=100, orient="horizontal")
  • Spinbox: Input widget for selecting from a range of values. It enables selecting of numeric values within a given range using up/down arrows or manual input.
tk.Spinbox(root, from_=0, to=10)
  • Toplevel: Creates a new independent window. It generates a new top-level window that can be independent of the main window.
tk.Toplevel(root)
  • PanedWindow: A window that contains panes, separated by a movable handle. It divides the window into resizable panes, allowing users to adjust pane sizes.
tk.PanedWindow(root, orient="horizontal")

Get ready for the high-paying data scientist jobs with these Top Data Science Interview Questions and Answers!

Geometry Methods of tkinter in Python 

In tkinter, the geometry managers – `pack()`, `grid()`, and `place()` – are used to arrange and position widgets within a window. These geometry management methods in tkinter provide different approaches for arranging and positioning widgets within a window, offering flexibility in designing the layout of graphical user interfaces in Python. Let’s understand each of these methods elaborately:

Python tkinter pack() method

The `pack()` method organizes widgets in blocks before placing them in the parent widget. Widgets are packed one after another, either horizontally or vertically.

Syntax:

widget_name.pack(options)

Options like `side`, `fill`, `expand`, `anchor`, and `padx`/`pady` can be used to control widget placement. Let’s take a look at one example:

import tkinter as tk

root = tk.Tk()

label1 = tk.Label(root, text="Intellipaat label - 1")

label1.pack()

label2 = tk.Label(root, text="Intellipaat label - 2")

label2.pack(side="bottom")

root.mainloop()

Output:

Python tkinter pack() method

Python grid() method

The `grid()` method arranges widgets in rows and columns using a table-like structure. Widgets are placed in rows and columns based on specified row and column indices.

Syntax:

widget.grid(options)

Options like `row`, `column`, `sticky`, `padx`/`pady`, `rowspan`, and `columnspan` control widget placement. Let’s take a look at one example:

import tkinter as tk

root = tk.Tk()

label1 = tk.Label(root, text="Intellipaat Label 1")

label1.grid(row=0, column=0)

label2 = tk.Label(root, text="Intellipaat Label 2")

label2.grid(row=1, column=0, sticky="W")

root.mainloop()

Output:

 Python grid() method

Python place() method

The `place()` method positions widgets using absolute or relative coordinates. Widgets are placed using specific x and y coordinates within the parent widget.

Syntax:

widget.place(options)

Options like `x`, `y`, `relx`, `rely`, `anchor`, and `width`/`height` control widget positioning. Let’s take a look at one example to better  understand this method():

import tkinter as tk

root = tk.Tk()

label1 = tk.Label(root, text="Intellipaat Label 1")

label1.place(x=20, y=30)

label2 = tk.Label(root, text="Intellipaat Label 2")

label2.place(relx=0.5, rely=0.5, anchor="center")

root.mainloop()

Output: 

Python place() method

Preparing for Job Interview? Refer to Python Interview Questions to excel in your Interviews!

Advantages of tkinter Module in Python

tkinter is a versatile and effortless module that supports developers in creating effective and efficient graphical user interfaces in Python. Here are some prominent advantages that tkinter offers:

Advantages of tkinter Library

Ease of Use: tkinter is user-friendly and relatively easy to learn, which makes it suitable for beginners. Its simplicity allows for quick prototyping and the development of GUI applications.

Platform Independence: tkinter is platform-independent, so applications developed with tkinter can run on various platforms such as Windows, macOS, and Linux without any change in code.

Rich Set of Widgets: Tkintertkinter provides a wide range of built-in widgets (buttons, labels, entry fields, etc.) for creating interactive interfaces. It also supports custom widget creation for more specialized needs.

Integration with Python: As tkinter is part of the standard Python library, it seamlessly integrates with other Python modules and libraries, facilitating easy data manipulation and application integration.

Event-Driven Programming: It supports event-driven programming that enables the handling of user interactions (such as mouse clicks and keyboard inputs) that update the interface accordingly.

Performance: For many applications, tkinter provides sufficient performance and responsiveness, which makes it suitable for a wide range of projects.

Wrapping up

tkinter is a part of Python’s standard library. It is a powerful yet accessible toolkit for crafting graphical user interfaces. Its simplicity and platform independence make it an ideal choice for beginners and developers. Offering a rich set of widgets, customizable appearances, and flexible layout managers, tkinter empowers users to create visually appealing applications efficiently.

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.