Importerror When Importing Tkinter in Python

ImportError when importing Tkinter in Python

The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.

To make your code work in both Python 2 and 3 you can do something like this:

try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *

However, PEP8 has this to say about wildcard imports:

Wildcard imports ( from <module> import * ) should be avoided

In spite of countless tutorials that ignore PEP8, the PEP8-compliant way to import would be something like this:

import tkinter as tk

When importing in this way, you need to prefix all tkinter commands with tk. (eg: root = tk.Tk(), etc). This will make your code easier to understand at the expense of a tiny bit more typing. Given that both tkinter and ttk are often used together and import classes with the same name, this is a Good Thing. As the Zen of python states: "explicit is better than implicit".

Note: The as tk part is optional, but lets you do a little less typing: tk.Button(...) vs tkinter.Button(...)

from Tkinter import * ImportError: No module named 'Tkinter'

Lets clear up some basics as it appears you think several things should work that never will.

No mater how you import you will always need to do Tk() with an upper case T by itself or with the appropriate prefix.

Things you have tried that will never work.

root = tk(), Tk.tk(), root = TK.TK()

all lower case tk() or all uppercase TK() will never work in tkinter.

If from tkinter import * doesn't work and doing top = tkinter.Tk() doesn't work it is very likely that you do not have tkinter installed. Or at least it has been removed for some reason.

Windows distro should come with tkinter already. I would try to do a clean install and see what happens. You should update to 3.6 anyway as 3.5 has some bugs that needed to be fixed.

As for your import issue.

from tkinter import * This line should work fine with top = Tk(). So that tells me tkinter is not installed.

import tkinter This redundant line should work as top = tkinter.Tk() but if the previous is not working then this likely wont either.

After doing some testing on PyCharm I can say that if PyCharm failed to load tkinter then it would have errored out first on the import and not the Tk() portion.

Traceback (most recent call last):
File "C:/Users/mcdomi3/PycharmProjects/MintyFlakes/test.py", line 1, in <module>
from Tkinter import *
ModuleNotFoundError: No module named 'tkinter'

Process finished with exit code 1

With that little revaluation I think your install is corrupt.

Conclusion.

You need to reinstall python or try to pip install tkinter as it is missing from your libraries or corrupt somehow.

ImportError: No module named 'Tkinter'

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

How can I fix the problem of importing tkinter module

All credit to this answer goes to @martineau and @stovfl, who posted the answers as comments.
One way to fix your problem is to reinstall python. This will redownload the Tkinter module as newer versions now auto download Tkinter.
You could also try installing it through the command prompt, like so:
python -m pip install tkinter or python -m easy_install tkinter
This will make sure that tkinter is installed on your computer and that you can import it properly.
Make sure to visit Import _tkinter or tkinter? for the difference between tkinter and _tkinter



Related Topics



Leave a reply



Submit