How to Fix Python ImportError: No Module Named _Tkinter, Please Install the Python-Tk Package

Zeeshan Afridi Feb 02, 2024
  1. the ImportError: No module named _tkinter, please install the python-tk package in Python
  2. Fix the ImportError: No module named _tkinter, please install the python-tk package in Python
How to Fix Python ImportError: No Module Named _Tkinter, Please Install the Python-Tk Package

This article will discuss the ImportError: No module named _tkinter, please install the python-tk package error in Python and how to fix it.

the ImportError: No module named _tkinter, please install the python-tk package in Python

Tkinter packages must be installed externally through the CLI and imported to your program. Otherwise, you will encounter the ImportError: No module named _tkinter, please install the python-tk package.

Let’s see the example.

Code:

import tkinter

Output:

ImportError: No module named _tkinter, please install the python-tk package
# or
ImportError: No module named _tkinter

Fix the ImportError: No module named _tkinter, please install the python-tk package in Python

To fix the error, install the tkinter package externally from the command line interface and then import it into the current program.

There might be a different version of the command based on your operating system (OS), so you can try the one that perfectly matches your OS.

If you have Python 3x then you need to run the following command. This command also works with Ubuntu.

sudo apt-get install python3-tk

Command - Fedora:

sudo dnf install python3-tkinter

Command - Arch Linux:

sudo pacman -S tk

Command - Debian-based and Python 3x:

sudo apt-get install python-tk

Moreover, if you are using RHEL, CentOS, or Oracle Linux, then you can use yum to install tkinter.

yum install tkinter

Based on your OS, you should install the version that suits your system, then import the tkinter into your current program, and it should work fine.

Let’s try a basic GUI application with tkinter.

Code:

from tkinter import *

window = Tk()

# label
lbl = Label(window, text="Welcome to DelfStack.com", fg="blue", font=("Helvetica", 14))
lbl.place(x=60, y=100)

# title
window.title("DelfStack")

# size of the dialog box
window.geometry("400x200+10+10")

window.mainloop()

Output:

GUI application using tkinter

The above code will create a small dialog box, which in this case displays a label "Welcome to DelfStack.com" and a title "DelfStack", but you can add more weights to it accordingly.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error