Import tkinter as tk ошибка

For some reason, I can’t use the Tkinter (or tkinter, on Python 3) module.
After running the following command in the python shell:

import Tkinter

or this, in Python 3:

import tkinter

I got this error

ModuleNotFoundError: No module named ‘Tkinter’

or this:

ModuleNotFoundError: No module named ‘tkinter’

What could be the reason for these errors and how can I solve it?

Ben the Coder's user avatar

asked Sep 18, 2014 at 6:19

RasmusGP's user avatar

0

You probably need to install it using something similar to the following:

  • For Ubuntu or other distros with Apt:

    sudo apt-get install python3-tk
    
  • For Fedora:

    sudo dnf install python3-tkinter
    

You can also mention a Python version number like this:

  • sudo apt-get install python3.7-tk
    
  • sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
    

Finally, import tkinter (for Python 3) or Tkinter (for Python 2), or choose at runtime based on the version number of the Python interpreter (for compatibility with both):

import sys
if sys.version_info[0] == 3:
    import tkinter as tk
else:
    import Tkinter as tk

Mark Amery's user avatar

Mark Amery

141k78 gold badges403 silver badges457 bronze badges

answered Sep 18, 2014 at 6:26

d-coder's user avatar

d-coderd-coder

12.5k4 gold badges25 silver badges36 bronze badges

4

As you are using Python 3, the module has been renamed to tkinter, as stated in the documentation:

Note Tkinter has been renamed to tkinter in Python 3. The 2to3 tool
will automatically adapt imports when converting your sources to
Python 3.

answered Sep 18, 2014 at 6:27

Burhan Khalid's user avatar

Burhan KhalidBurhan Khalid

169k18 gold badges243 silver badges282 bronze badges

0

If you’re using python 3.9 on Mac, you can simply install tkinter using brew:

brew install python-tk@3.9

This fixed it for me.

Edit:
As mentioned by others, you can also use the general command to install the latest version:

brew install python-tk

answered Apr 10, 2021 at 18:11

P1NHE4D's user avatar

P1NHE4DP1NHE4D

1,0468 silver badges14 bronze badges

0

For windows 10, it is important to check in the Python install the optional feature «tcl/tk and IDLE». Otherwise you get a ModuleNotFoundError: No module named ‘tkinter’. In my case, it was not possible to install tkinter after the Python install with something like «pip install tkinter»

answered Jan 29, 2020 at 15:43

Andi Schroff's user avatar

Andi SchroffAndi Schroff

1,1281 gold badge11 silver badges18 bronze badges

3

To install the Tkinter on popular Linux distros:

Debian/Ubuntu:

sudo apt install python3-tk -y  

Fedora:

sudo dnf install -y python3-tkinter

Arch:

sudo pacman -Syu tk --noconfirm 

REHL/CentOS6/CentOS7:

sudo yum install -y python3-tkinter

OpenSUSE:

sudo zypper in -y python-tk

bfontaine's user avatar

bfontaine

17.9k13 gold badges71 silver badges103 bronze badges

answered Feb 20, 2021 at 13:31

amzy-0's user avatar

amzy-0amzy-0

3753 silver badges5 bronze badges

5

You might need to install for your specific version, I have known cases where this was needed when I was using many versions of python and one version in a virtualenv using for example python 3.7 was not importing tkinter I would have to install it for that version specifically.

For example

sudo apt-get install python3.7-tk 

No idea why — but this has occured.

answered May 20, 2020 at 17:31

deMangler's user avatar

deManglerdeMangler

4173 silver badges14 bronze badges

0

For Mac use:

brew install python-tk

imxitiz's user avatar

imxitiz

3,9102 gold badges9 silver badges33 bronze badges

answered Jan 21, 2022 at 21:24

Diego Medeiros's user avatar

0

Installing Tkinter

python -m pip install tk-tools

or

sudo apt install python3-tk

answered Sep 26, 2021 at 13:11

Mr.Programmer nope's user avatar

3

For Windows 10 using either VSCode or PyCharm with Python 3.7.4 — make sure Tk is ticked in the install. I tried import tkinter as xyz with upper/lower t and k‘s and all variants without luck.

What works is:

import tkinter
import _tkinter
tkinter._test()

An example in action:

import tkinter
import _tkinter

HEIGHT = 700
WIDTH = 800

root = tkinter.Tk()

canvas = tkinter.Canvas(root, height = HEIGHT, width=WIDTH)
canvas.pack()

frame = tkinter.Frame(root, bg='red')
frame.pack()

root.mainloop()

answered Sep 7, 2019 at 3:29

Jeremy Thompson's user avatar

Jeremy ThompsonJeremy Thompson

61.1k33 gold badges186 silver badges318 bronze badges

4

check the python version you have installed by using command python --version

check for the Tk module installed correctly from following code

sudo apt-get install python3-tk 

Check if you are using open-source OS then

check the tkinter module in the following path
/home/python/site-packages/tkinter
change the path accordingly your system

barbsan's user avatar

barbsan

3,41811 gold badges21 silver badges28 bronze badges

answered Jul 24, 2019 at 9:56

Devaliya Pratik's user avatar

On CentOS7, to get this working with Python2, I had to do:

yum -y install tkinter

Noting this here because I thought that there would be a pip package, but instead, one needs to actually install an rpm.

answered Feb 3, 2020 at 19:57

Frederick Ollinger's user avatar

Make sure that when you are running your python code that it is in the python3 context. I had the same issue and all I had to do was input the command as:

sudo python3 REPLACE.py

versus

sudo python REPLACE.py

the latter code is incorrect because tkinter is apparently unnavailable in python1 or python2.

answered Nov 28, 2019 at 20:42

Owen Preece's user avatar

3

You just need to install it and import them your project like that :

this code import to command line :

sudo apt-get install python3-tk 

after import tkinter your project :

from tkinter import *

answered Dec 8, 2019 at 6:33

Jafar Choupan's user avatar

1

I resolved my issue in the PyCharm do following

  1. Install Python Interpreter from https://www.python.org/
  2. PyCharm > Preferences > Python Interpreter > Add
  3. Select installed interpreter
  4. In the run configuration select the newly installed interpreter

I also made a video instruction what I did https://youtu.be/awaURBnfwxk

answered Nov 25, 2021 at 15:50

Dima Portenko's user avatar

Dima PortenkoDima Portenko

3,3745 gold badges33 silver badges48 bronze badges

For Windows I had to reinstall python and make sure that while installing in Optional Features I had «tcl/tk and IDLE» enabled.

answered Sep 2, 2022 at 15:45

Sout parl's user avatar

Tkinter should come with the latest Python, I don’t think it comes with Python2. I had the same problem but once. I upgraded to Python 3.8 Tkinter was installed.

Sabito stands with Ukraine's user avatar

answered Jun 10, 2020 at 14:14

Eloni's user avatar

EloniEloni

294 bronze badges

$ sudo apt-get install python3.10-tk

answered Jan 14, 2022 at 10:20

Udesh's user avatar

UdeshUdesh

2,1672 gold badges18 silver badges30 bronze badges

tkinter comes with python… uninstall python, reinstall it, you’re done

answered Apr 12, 2020 at 5:54

PythonProgrammi's user avatar

PythonProgrammiPythonProgrammi

22.2k3 gold badges40 silver badges34 bronze badges

0

if it doesnot work in pycharm you can add the module in the project interpreter by searching in +button python-tkinter and download it.

answered Jul 12, 2020 at 4:27

santosh ghimire's user avatar

Check apt for tasks, it may be marked for removed

sudo apt autoremove

Then check and install needed

answered Apr 23, 2020 at 6:02

Sergey Shamanayev's user avatar

We can use 2 types of methods for importing libraries

  1. work with import library
  2. work with from library import *

You can load tkinter using these ways:

  1. from tkinter import*

  2. import tkinter

Delrius Euphoria's user avatar

answered May 5, 2021 at 11:16

رضا آلناصر's user avatar

On Linux it is possible to have installed two different versions of Python in my case 3.11 and 3.10. Only 3.10 was working with tkinter. 3.10 binary was located in my /usr/bin/python3 and 3.11 was located in /usr/local/sbin/python3. You can either specifically source the version you need or if you are SURE you don’t need 3.11 at the moment, you can sudo cp /usr/bin/python3 /usr/local/sbin/python3 assuming your working version is in bin like mine is.

answered Mar 3 at 2:03

Poseidon's user avatar

PoseidonPoseidon

1551 silver badge8 bronze badges

try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter 
except ImportError:
    try:
        # for Python3
        from tkinter import *   ## notice lowercase 't' in tkinter here
    except:
        try:
            print "Download Tkinter" ##python 2
        except SyntaxError:
            print("Download Tkinter") ##python 3

answered Mar 31, 2021 at 2:43

Rodrigo Alvaro Santo SD 6's user avatar

0

If you have pip on your path, you could (in your command prompt) just type
pip install tkinter
Most versions of python already come with tkinter.

answered Feb 8, 2022 at 20:40

GL32's user avatar

1

You’ll probably need to install Tkinter.
You can do so like this in the Windows command prompt:

pip install tk

answered Jan 25 at 6:26

AngusAU293's user avatar

——— WORKED ON PYTHON 2.7————

Install all below packages

sudo apt-get install git
sudo apt-get install python-tk
sudo apt-get install python-pip
sudo apt install picolisp
sudo -H pip2 install --upgrade pip
sudo pip install -I pillow
sudo apt-get install python-imaging-tk
sudo apt-get install python-tk

answered May 4, 2020 at 11:11

RAHUL 's user avatar

RAHUL RAHUL

234 bronze badges

1

Firstly you should test your python idle to see if you have tkinter:

import tkinter

tkinter._test()

Trying typing it, copy paste doesn’t work.

So after 20 hours of trying every way that recommended on those websites figured out that you can’t use «tkinter.py» or any other file name that contains «tkinter..etc.py». If you have the same problem, just change the file name.

fcdt's user avatar

fcdt

2,3315 gold badges12 silver badges26 bronze badges

answered Sep 20, 2020 at 9:38

Mehmet Nergiz's user avatar

cmd — terminal

pip install tkinter

answered Jan 22, 2022 at 9:10

Egemen İm's user avatar

1

Unused import(s) enum, sys, types, TclError, re, wantobjects, TkVersion, TclVersion, READABLE, WRITABLE, EXCEPTION, EventType, Event, NoDefaultRoot, Variable, StringVar, IntVar, DoubleVar, BooleanVar, mainloop, getint, getdouble, getboolean, Misc, CallWrapper, XView, YView, Wm, Tcl, Pack, Place, Grid, BaseWidget, Widget, Toplevel, Canvas, Checkbutton, Entry, Frame, Label, Listbox, Menu, Menubutton, Message, Radiobutton, Scale, Scrollbar, Text, OptionMenu, Image, PhotoImage, BitmapImage, image_names, image_types, Spinbox, LabelFrame, PanedWindow, NO, FALSE, OFF, YES, TRUE, ON, N, S, W, E, NW, SW, NE, SE, NS, EW, NSEW, CENTER, NONE, X, Y, BOTH, LEFT, TOP, RIGHT, BOTTOM, RAISED, SUNKEN, FLAT, RIDGE, GROOVE, SOLID, HORIZONTAL, VERTICAL, NUMERIC, CHAR, WORD, BASELINE, INSIDE, OUTSIDE, SEL, SEL_FIRST, SEL_LAST, END, INSERT, CURRENT, ANCHOR, ALL, NORMAL, DISABLED, ACTIVE, HIDDEN, CASCADE, CHECKBUTTON, COMMAND, RADIOBUTTON, SEPARATOR, SINGLE, BROWSE, MULTIPLE, EXTENDED, DOTBOX, UNDERLINE, PIESLICE, CHORD, ARC, FIRST, LAST, BUTT, PROJECTING, ROUND, BEVEL, MITER, MOVETO, SCROLL, UNITS and PAGES from wildcard import of tkinter

When running Python on Linux-based OSes, you may encounter an error that says:

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

This error happens because the Tkinter package can’t be found on your computer system.

There are several solutions you can try to fix this error, based on the Operating System you used:

Fix No module named _tkinter on Linux

To fix this error on Linux-based OS, you need to install python-tk on your system.

Run the following command from the terminal:

# For Python 2:
sudo apt-get install python-tk

# For Python 3:
sudo apt-get install python3-tk

# CentOS alternative:
sudo yum install tkinter

# Fedora alternative:
sudo dnf install python3-tkinter

Once the Tkinter package was installed, you can import the package into your code.

Fix No module named _tkinter on macOS

If you see this error while running Python on macOS, then you need to install python-tk with homebrew:

Once you installed the package, you should be able to import tkinter in your code:

import tkinter

tkinter._test()

Fix No module named _tkinter on Windows

This error shouldn’t happen in Windows because Tkinter was bundled with Python.

But if you see this error on Windows, then try installing the tk package with pip:

pip install tk

# or

pip3 install tk

If that doesn’t work, run Python .exe installer and select the Modify menu.

You need to make sure that the tcl/tk feature is selected as follows:

Finish the installation setup and you should be able to use the Tkinter package now.

Make sure that the import statement is correct

If you still see this error after installing Tkinter, then you may have an import statement that’s incompatible with your Python version.

In Python version 2, you need to import Tkinter with a capital T. In Python 3, you import the package will all lowercase as import tkinter.

You can use a try-except block to make your code runs in both Python 2 and 3 as follows:

try:
    # For Python 2
    import Tkinter as tk
except ImportError:
    # For Python 3
    import tkinter as tk

tk._test()

The code above instructs Python to import the tkinter package when the Tkinter package was not found.

Don’t forget to add the same alias so that the code you write still works no matter which library was imported.

Conclusion

The error ImportError: No module named _tkinter occurred because the Tkinter package was not found on your computer system.

Several Linux-based distributions separate the Tkinter package from the standard libraries included in Python, so you need to install it manually.

Once Tkinter is installed, this error should be resolved.

I hope this article was helpful. Happy coding! 👍

Tkinter is a built-in user interface module in Python. When the error ModuleNotFoundError: No module named ‘tkinter’ in Python appears, it may be because the module is faulty or not installed. This article will show you how to reinstall it on different operating systems.

What causes the error ModuleNotFoundError: No module named ‘tkinter’ in Python

The cause of this error may be that the Tkinter module has not been installed in the Python environment. Let’s look at the causes of this error on different operating systems.

The cause of the error occurs on macOS.

The cause of the error on the macOS operating system can happen because we install Tkinter with the command “pip

Example:

pip install tk

Sometimes the “pip” command won’t work as expected on macOS.

The cause of the error occurs on Windows.

Tkinter is already available in Python. This error may be because it has been deleted or corrupted. The error will be displayed as follows.

ModuleNotFoundError: No module named 'tkinter'

The cause of the error occurs on Linux.

The problem ModuleNotFoundError: No module called ‘tkinter’ on Linux can potentially be caused by this module not being installed into the Python environment, or by typing the incorrect module name since in Python 3 it has been changed to lowercase “tkinter,” instead of “Tkinter” in Python 2.

In Python version 2:

import Tkinter as tk

In Python version 3:

import tkinter as tk

The solution is straightforward: install Tkinter or reinstall Python. Tkinter is already available in Python and needs to be aware of the name when importing in different Python versions. Here is the workaround for each different operating system.

Reinstall on macOS operating system

On macOS, we should use “brew” to install Tkinter. “brew” can also use to install all kinds of software packages on macOS.

brew install python-tk

Reinstall on Windows operating system

Reinstall Python by downloading the required version. Remember to check “Add Python to PATH” and “tcl/tk and IDLE” when installing. Then just import and use.

Example:

from tkinter import *

window = Tk()
window.title("learnshareit")
window.mainloop()

Reinstall on Linux operating system

On Linux operating system, we can reinstall using the following syntax

Ubuntu or Debian operating systems

sudo apt-get install python3-tk

CentOS operating systems

sudo yum install python3-tkinter

Fedora operating systems

sudo dnf install python3-tkinter

To check if Tkinter is installed on Linux, we use this command.

python3 -m tkinter

If you see the message ModuleNotFoundError: No module named ‘tkinter’ in Python means that Tkinter is not installed yet.

You can review a similar article about this error for a better understanding.

Summary

Through the article, we have understood why the error occurs and how to solve it on different operating systems. The fix is straightforward. We need to reinstall Python, and Python already has Tkinter. Hope this article was helpful to you. Thanks for reading.

Maybe you are interested:

  • ModuleNotFoundError: No module named ‘PyQt5’ in Python
  • ModuleNotFoundError: No module named ‘pandas’ in Python
  • No module named ‘google.cloud’ in Python

Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).

Bug report

I just wanted to use matplotlib to draw a line graph and save it to a file, but every time I try to import the pyplot module I get the abovementioned error message.

python3 -c "import matplotlib.pyplot"

Actual outcome

>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python3.6/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/usr/local/lib/python3.6/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.6/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

Expected outcome

Whatever happens when import matplotlib.pyplot succeeds

Matplotlib version

  • Operating System: Windows Subsystem for Linux
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6.0

I installed matplotlib using pip.
Doing sudo apt-get install python3-tk, as recommended by someone on Stack Overflow, didn’t help.

Понравилась статья? Поделить с друзьями:
  • Import seaborn as sns ошибка
  • Import requests python 3 ошибка
  • Import react from react ошибка
  • Import pygame ошибка в pycharm
  • Import org openqa selenium webdriver ошибка