Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named 'Tkinter'

Tags:

python

tkinter

For some reason, I can't use the Tkinter or tkinter module. After running the following command in the python shell

import Tkinter

or

import tkinter

I got this error

ModuleNotFoundError: No module named 'Tkinter'

or

ModuleNotFoundError: No module named 'tkinter'

What could be the reason for and how can we solve it?

like image 531
RasmusGP Avatar asked Sep 18 '14 06:09

RasmusGP


People also ask

Why does it say no module named tkinter?

The Python "ModuleNotFoundError: No module named 'tkinter'" occurs when tkinter is not installed in our Python environment. To solve the error, install the module and import is as import tkinter as tk .

Why is my tkinter not working?

The easiest way to fix this problem is by upgrading your python to use python 3. If upgrading python is not an option, you only need to rename your imports to use Tkinter (uppercase) instead of tkinter (lowercase). Output: As a result, this window proves that your python installation includes the Tkinter module.

How do I install tkinter?

The simplest method to install Tkinter in a Windows environment is to download and install either ActivePython 3.8 or 3.7 from here. Alternatively, you can create and activate a Conda environment with Python 3.7 or greater that is integrated with the latest version of Tkinter.

How do I know if I have tkinter installed?

Running python -m tkinter from the command line should open a window demonstrating a simple Tk interface, letting you know that tkinter is properly installed on your system, and also showing what version of Tcl/Tk is installed, so you can read the Tcl/Tk documentation specific to that version.


3 Answers

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
like image 117
d-coder Avatar answered Oct 23 '22 23:10

d-coder


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.

like image 39
Burhan Khalid Avatar answered Oct 24 '22 00:10

Burhan Khalid


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

brew install [email protected]

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
like image 21
P1NHE4D Avatar answered Oct 24 '22 00:10

P1NHE4D