Signup/Sign In

Introduction to Tkinter Module in Python

Tkinter module in Python is a standard library in Python used for creating Graphical User Interface (GUI) for Desktop Applications. With the help of Tkinter developing desktop applications is not a tough task.

The Tkinter module in Python is a good way to start creating simple projects in Python

Before starting with Tkinter you should have basic knowledge of Python. You can learn Python using our Complete Python Tutorial.

The Tkinter library provides us with a lot of built-in widgets (also called Tk widgets or Tk interface) that can be used to create different desktop applications.

What is Tkinter?

Tkinter in Python helps in creating GUI Applications with minimum hassle. Among various GUI Frameworks, Tkinter is the only framework that is built-in into Python's Standard Library.

  • An important feature in favor of Tkinter is that it is cross-platform, so the same code can easily work on Windows, macOS, and Linux.

  • Tkinter is a lightweight module.

  • It comes as part of the standard Python installation, so you don't have to install it separately.

  • It supports a lot of built-in widgets that can be used directly to create desktop applications.

What are Tcl, Tk, and Tkinter?

Let's try to understand more about the Tkinter module by discussing more about its origin.

  • Tkinter is based upon the Tk toolkit, which was originally designed for the Tool Command Language (Tcl). As Tk is very popular thus it has been ported to a variety of other scripting languages, including Perl (Perl/Tk), Ruby (Ruby/Tk), and Python (Tkinter).

  • The wide variety of widgets, portability, and flexibility of Tk makes it the right tool which can be used to design and implement a wide variety of simple and complex projects.

  • Python with Tkinter provides a faster and more efficient way to build useful desktop applications that would have taken much time if you had to program directly in C/C++ with the help of native OS system libraries.

How to install Tkinter?

Tkinter comes as part of standard Python installation. So if you have installed the latest version of Python, then you do not have to do anything else.

If you do not have Python installed on your system - Install Python (the current version is 3.11.4 at the time of writing this article) first, and then check for Tkinter.

You can determine whether Tkinter is available for your Python interpreter by attempting to import the Tkinter module.

import tkinter

If Tkinter is available, then there will be no errors, otherwise, you will see errors in the console.

Using Tkinter to Create Desktop Applications

The basic steps of creating a simple desktop application using the Tkinter module in Python are as follows:

  1. First of all, import the Tkinter module.

  2. The second step would be to create a basic window for the desktop application.

  3. Then you can add different GUI components to the window and functionality to these components or widgets.

  4. Then enter the main event loop using mainloop() function to run the Desktop application.

So let's write some code and create a basic desktop application using the Tkinter module in Python.

Hello World tkinter Example

  • When you create a desktop application, the first thing that you will have to do is create a new window for the desktop application.

  • The main window object is created by the Tk class in Tkinter.

  • Once you have a window, you can text, input fields, buttons, etc. to it.

Here's a code example,

import tkinter as tk

win = tk.Tk() 
win.title('Hello World!')

# you can add widgets here

win.mainloop()

tkinter module in Python

The window may look different depending on the operating system.

Tkinter Methods used above:

The two main methods that are used while creating desktop applications in Python are:

1. Tk( )

The syntax for the Tk() method is:

Tk(screenName=None, baseName=None, className='Tk', useTk=1)

This method is used to create the main window.

This is how you can use it, just like in the Hello World code example,

win = tkinter.Tk()    ## where win indicates name of the main window object

2. The mainloop() Function

This method is used to start the application. The mainloop() function is an infinite loop that is used to run the application.

It will wait for events to occur and process the events as long as the window is not closed.

Common Tkinter Widgets

Here are some of the commonly used Tkinter widgets:

  1. Button: A simple button that can be clicked to perform an action.

  2. Label: This is used to display simple text.

  3. Entry: Simple input field to take input from the user. This is used for single-line input.

  4. Text: This can be used for multi-line text fields, to show text, or to take input from the user.

  5. Canvas: This widget can be used for drawing shapes, images, and custom graphics.

  6. Frame: This acts as a container widget that can be used to group other widgets together.

  7. Checkbutton: A checkbox widget used for creating toggle options on or off.

  8. Radiobutton: This is used to create Radio buttons.

  9. Listbox: This is used to create a List widget used for displaying a list of values.

  10. Scrollbar: This widget is used for scrolling content in other widgets like Text and Listbox.

Here's a simple example where we have used some of the widgets,

import tkinter as tk

root = tk.Tk()
root.title("Tkinter World")

label = tk.Label(root, text="Hello, Studytonight!")
label.pack()

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

button = tk.Button(root, text="Click Me!")
button.pack()

root.mainloop()

Tkinter simple example

Alternatives of Tkinter

There are many ways to develop GUI-based projects and applications in Python. These different ways are given below:

1. PyQt/PySide:

  • PyQt and PySide are based on the Qt framework.

  • It can be used for both desktop and mobile platforms.

  • It is Free and open-source.

2. wxPython:

This is an open-source, cross-platform GUI toolkit that is written in C++. Also an alternative to Tkinter.

3. Kivy:

  • This is good for creating multi-touch support modern applications for Mobile and Tablets.

  • You can create games and applications for modern mobile devices using it.

4. GTK+ (PyGTK):

You can use this to create GTK-based applications.

5. Toga:

It is a native, multi-platform toolkit that can be used to create good desktop applications.

6. Remi:

You can create web applications using Python with the help of Remi. You can create browser-based applications without any knowledge of HTML, CSS, or JavaScript.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight