Introduction to Python Tkinter Module
In this tutorial, we will cover an introduction to Tkinter, its prerequisites, different ways for GUI Programming, how to install Tkinter, and its working.
Tkinter 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 primary GUI toolkit we will be using is
Tk, which is Python's default GUI library. We'll access
Tk from its Python interface called Tkinter (short for Tk interface).
Prerequisites for Tkinter
Before learning Tkinter you should have basic knowledge of Python. You can learn Python using our Complete Python Tutorial.
GUI Programming in Python
There are many ways to develop GUI based programs in Python. These different ways are given below:
In Python, Tkinter is a standard GUI (graphical user interface) package. Tkinter is Python's default GUI module and also the most common way that is used for GUI programming in Python. Note that Tkinter is a set of wrappers that implement the
Tk widgets as Python classes.
This is basically an open-source, cross-platform GUI toolkit that is written in C++. Also an alternative to Tkinter.
JPython is a Python platform for Java that is providing Python scripts seamless access to Java class Libraries for the local machine.
We will cover GUI Programming with Tkinter.
What is Tkinter?
Tkinter in Python helps in creating GUI Applications with a 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 is simple to use.
What are Tcl, Tk, and Tkinter?
Let's try to understand more about the Tkinter module by discussing more about it origin.
As mentioned, Tkinter is Python's default GUI library, which is nothing but a wrapper module on top of the Tk toolkit.
Tkinter is based upon the Tk toolkit, and 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).
GUI development portability and flexibility of Tk makes it the right tool which can be used to design and implement a wide variety of commercial-quality GUI applications.
Python with Tkinter provides us a faster and efficient way in order to build useful applications that would have taken much time if you had to program directly in C/C++ with the help of native OS system libraries.
Once we have Tkinter up and running, we'll use basic building blocks known as widgets to create a variety of desktop applications.
Chances are, that Tkinter may be already installed on your system along with Python. But it is not true always. So let's first check if it is available.
If you do not have Python installed on your system - Install Python 3.8 first, and then check for Tkinter.
You can determine whether Tkinter is available for your Python interpreter by attempting to import the Tkinter module - If Tkinter is available, then there will be no errors, as demonstrated in the following code:
Nothing exploded, so we know we have Tkinter available. If you see any error like module not found, etc, then your Python interpreter was not compiled with Tkinter enabled, the module import fails and you might need to recompile your Python interpreter to gain access to Tkinter.
Tk to your Applications
Basic steps of setting up a GUI application using Tkinter in Python are as follows:
First of all, import the Tkinter module.
The second step is to create a top-level windowing object that contains your entire GUI application.
Then in the third step, you need to set up all your GUI components and their functionality.
Then you need to connect these GUI components to the underlying application code.
Then just enter the main event loop using
The above steps may sound gibberish right now. But just read them all, and we will explain everything as we move on with this tutorial.
First Tkinter Example
As mentioned earlier that in GUI programming all main widgets are only built on the top-level window object.
The top-level window object is created by the
Tk class in
Let us create a top-level window:
import tkinter as tk
win = tk.Tk()
###you can add widgets here
Tkinter Methods used above:
The two main methods are used while creating the Python application with GUI. You must need to remember them and these are given below:
1. Tk(screenName=None, baseName=None, className='Tk', useTk=1)
This method is mainly used to create the main window. You can also change the name of the window if you want, just by changing the className to the desired one.
The code used to create the main window of the application is and we have also used it in our above example:
win = tkinter.Tk() ## where win indicates name of the main window object
This method is used to start the application. The
mainloop() function is an infinite loop which is used to run the application, it will wait for an event to occur and process the event as long as the window is not closed.
With this we have completed the introduction to Tkinter, we have installed the Tkinter module, and even know what are Windows and Widgets in Tkinter. We also create our first Tkinter GUI app and run it. In the next tutorial, we will learn more about Python Tkinter widgets.