问题:ImportError:没有名为“ Tkinter”的模块

由于某些原因,我无法使用Tkinteror tkinter模块。在python shell中运行以下命令后

import Tkinter

要么

import tkinter

我得到这个错误

ModuleNotFoundError:没有名为“ Tkinter”的模块

要么

ModuleNotFoundError:没有名为“ 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?


回答 0

您可能需要使用以下一种(或类似的方式)安装它:

sudo apt-get install python3-tk 

sudo dnf install python3-tkinter

我已经使用Tkinter一段时间了。您为什么不尝试一下,让我知道它是否有效?

try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter 
except ImportError:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here

这是参考链接,这是文档

最好按照这里的建议检查版本:

if sys.version_info[0] == 3:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here
else:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter

否则你会得到错误 ImportError: No module named tkinter

编辑1:为了使这个答案更通用,我从评论中借用了以下内容。在Fedora上,请使用以下任一命令

sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64

要么

sudo dnf install python3-tkinter

You probably need to install it using one of (or something similar to) the following:

sudo apt-get install python3-tk 

You can also mention version number like this sudo apt-get install python3.7-tk for python 3.7.

sudo dnf install python3-tkinter

Why don’t you try this and let me know if it worked:

try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter 
except ImportError:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here

Here is the reference link and here are the docs

Better to check versions as suggested here:

if sys.version_info[0] == 3:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here
else:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter

Or you will get an error ImportError: No module named tkinter


Just to make this answer more generic I borrowed the following from Devendra Bhat‘s comment:

On Fedora please use either of the following commands

sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64

or

sudo dnf install python3-tkinter

回答 1

在使用Python 3时,该模块已重命名为tkinter,如文档中所述:

注意Tkinter在Python 3中已重命名为tkinter。将源转换为Python 3时,2to3工具将自动适应导入。

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.


回答 2

对于Windows 10,重要的是要在Python中签入安装可选功能“ tcl / tk和IDLE”。否则,您将收到ModuleNotFoundError:没有名为’tkinter’的模块。就我而言,在Python安装后无法通过“ pip install tkinter”之类的方式安装tkinter

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”


回答 3

对于 使用VSCode或PyCharm和Python 3.7.4的Windows 10-确保在安装中勾选了Tk。我尝试import tkinter as xyz使用较高/较低的tk以及所有没有运气的变体

起作用的是:

import tkinter
import _tkinter
tkinter._test()

一个实际的例子:

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()

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()

回答 4

您可能需要为您的特定版本安装,当我使用许多版本的python,而在virtualenv中使用例如python 3.7的一个版本未导入tkinter时,我已经知道需要这样做,因此我必须为该版本安装它特别。

例如

sudo apt-get install python3.7-tk 

不知道为什么-但这已经发生了。

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.


回答 5

确保在运行python代码时,它位于python3上下文中。我遇到了同样的问题,我要做的就是输入以下命令:

sudo python3 REPLACE.py

sudo python REPLACE.py

后一个代码不正确,因为tkinter在python1或python2中显然不可用。

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.


回答 6

使用命令检查您已安装的python版本 python --version

通过以下代码检查Tk模块是否正确安装

sudo apt-get install python3-tk 

然后检查您是否正在使用开源操作系统

检查以下路径中的tkinter模块 / home / python / site-packages / tkinter相应地 更改路径

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


回答 7

您只需要安装它,然后将其导入项目,如下所示:

此代码导入命令行:

sudo apt-get install python3-tk 

导入tkinter后,您的项目:

from tkinter import *

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 *

回答 8

在CentOS7上,要使其与Python2一起使用,我必须做:

yum -y install tkinter

在这里指出这一点是因为我认为会有一个pip包,但是实际上需要安装一个rpm。

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.


回答 9

tkinter是python附带的…卸载python,重新安装,就完成了

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


回答 10

检查易于执行的任务,可能会将其标记为已删除

sudo apt autoremove

然后检查并安装所需

Check apt for tasks, it may be marked for removed

sudo apt autoremove

Then check and install needed


回答 11

———在PYTHON 2.7上工作————

安装以下所有软件包

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

——— 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

回答 12

tkinter应该带有最新的python,我不认为它是python 2附带的。我有同样的问题,但是一旦我升级到python 3.8,就安装了tkinter

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.


回答 13

在下面使用。

from tkinter import *
root=Tk()
.....
root.mainloop()

use below.

from tkinter import *
root=Tk()
.....
root.mainloop()

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。