Can't import tkinter (or Tkinter)

2019-01-20 10:53发布

I am trying to import Tkinter to my project using Python 2.7 and instead I get the error:

ImportError: No module named tkinter

Before anyone says it, I have tried both "Tkinter" and "tkinter" but have gotten the exact same message.

5条回答
萌系小妹纸
2楼-- · 2019-01-20 11:06

If you are using Ubuntu or Debian OS try this:-

sudo apt-get install python-tk

Or if you are using Python 3:-

sudo apt-get install python3-tk
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-20 11:13

You should install the tkinter package for python. Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

Note: The following commands below are assuming that you run a Linux device.

For Python 2:

pip install python-tk
sudo apt-get install python-tk

for Python 3:

pip install python3-tk
sudo apt-get install python3-tk
查看更多
Fickle 薄情
4楼-- · 2019-01-20 11:21

First try using this code for your imports.

try:
    import Tkinter as tk # this is for python2
except:
    import tkinter as tk # this is for python3

If this doesn't work, try reinstalling tkinter. If you don't know how to reinstall tkinter look at the tkinter installation page, here.

查看更多
成全新的幸福
5楼-- · 2019-01-20 11:27

Had the same problem ,please try this;

from Tkinter import *
查看更多
乱世女痞
6楼-- · 2019-01-20 11:33

Some compilers have tkinter preinstalled. For example, if you use IDLE tkinter is preinstalled. As much as I know, if you use IDLE, you have to click a box in order to install tkinter. If you are not using IDLE, check whether tkinter/Tkinter is included in your Site Packages folder. Consider reinstalling the compiler/interpreter you are using. After you made sure it is installed the syntax you have to use depends on the version of Python you are using. I am not quite sure for Python 2, but I think you write:

import Tkinter

For Python 3 you write:

import tkinter

or the more often used:

from tkinter import * 
查看更多
登录 后发表回答