Tkinter is a Python library that is used to create graphical user interfaces (GUIs). It is a standard Python interface to the Tk GUI toolkit, which is widely used in the Linux operating system. In this tutorial, we will learn how to install Tkinter on a Linux system using either pip or apt-get.
To install Python’s Tkinter module on a Linux system, you will need to have the following prerequisites installed:
- Python: Tkinter is a module for Python, so you will need to have Python installed on your system. Most Linux distributions come with Python pre-installed, but if you don’t have it installed you can use your distribution’s package manager to install it.
- Tcl/Tk: Tkinter is built on top of the Tcl/Tk libraries, so you will need to have these libraries installed in order to use Tkinter. Most Linux distributions come with Tcl/Tk pre-installed, but if you don’t have them installed you can use your distribution’s package manager to install them.
- A development environment: In order to install Tkinter, you will need to have a development environment set up on your system. This typically includes a compiler (such as GCC) and the make utility. If you don’t have these tools installed, you can use your distribution package manager to install them.
Install Tkinter in Linux
Python Tkinter library packages are available under the default package repositories. You can install it using the system’s package manager to install the Python Tkinter module based on the operating system.
- On Debian-based Linux (Ubuntu, Debian, Pop!_OS):
sudo apt-get install python3-tk
- On Arch-based Linux systems:
sudo pacman -S tk
- On CentOS, RedHat, and Oracle Linux systems:
sudo yum install -y tkinter tk-devel
- On Fedora-based Linux systems:
sudo dnf install python3-tkinter
This will install the Tkinter module on your system.
Verify the Tkinter installation
Once the Python Tkinter module is successfully installed on your system. The following command will show you the Tkinter versions.
python3 -m tkinter
The below screenshot shows that Python Tkinter 8.6 is installed.
Running an Example with Tkinter
Let’s make a sample Python program using the Tkinter module. Create a file `example.py` file with the following content.
Now, run the Python script with the following command.
You should see the following result.
In this tutorial, we learned how to install Tkinter on a Linux system using either pip or apt-get. We also learned how to import and use Tkinter in our Python scripts. Tkinter is a powerful library that allows us to create attractive and functional GUI applications in Python, and it is widely used in the Linux ecosystem. With this knowledge, you should be able to use Tkinter to create your own GUI applications on a Linux system.