Python 导入错误:没有名为“Tkinter”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25905540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:48:37  来源:igfitidea点击:

ImportError: No module named 'Tkinter'

pythontkinter

提问by RasmusGP

For some reason, I can't use the Tkinteror tkintermodule. After running the following command in the python shell

出于某种原因,我不能使用Tkinterortkinter模块。在python shell中运行以下命令后

import Tkinter

or

或者

import tkinter

I got this error

我收到这个错误

ModuleNotFoundError: No module named 'Tkinter'

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

or

或者

ModuleNotFoundError: No module named 'tkinter'

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

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

可能是什么原因以及我们如何解决它?

采纳答案by d-coder

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

您可能需要使用以下之一(或类似的东西)安装它:

sudo apt-get install python3-tk 

sudo dnf install python3-tkinter

I have been using Tkinter for a while now. Why don't you try this and let me know if it worked?

我已经使用 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

Here is the reference linkand here is the doc

这是参考链接,这是文档

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 error ImportError: No module named tkinter

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

EDIT 1: Just to make this answer more generic I borrowed the following from the comments. On Fedora please use either of the following commands

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

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

or

或者

sudo dnf install python3-tkinter

回答by Burhan Khalid

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

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

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

注意 Tkinter 在 Python 3 中已重命名为 tkinter。 2to3 工具将在将您的源代码转换为 Python 3 时自动调整导入。

回答by Devaliya Pratik

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

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

check for the Tk module installed correctly from following code

从以下代码检查是否正确安装了 Tk 模块

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/tkinterchange the path accordingly your system

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

回答by Jeremy Thompson

For Windows 10using either VSCode or PyCharm with Python 3.7.4 - make sure Tk is ticked in the install. I tried import tkinter as xyzwith upper/lower tand k's and all variantswithout luck.

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

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

回答by Owen Preece

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

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

sudo python3 REPLACE.py

versus

相对

sudo python REPLACE.py

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

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

回答by Jafar Choupan

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 :

导入 tkinter 您的项目后:

from tkinter import *

回答by sameer_nubia

use below.

下面使用。

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

回答by Andi Schroff

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"

对于 Windows 10,重要的是在 Python 安装中检查可选功能“tcl/tk 和 IDLE”。否则你会得到一个ModuleNotFoundError: No module named 'tkinter'。就我而言,在 Python 安装后无法使用“pip install tkinter”之类的东西安装 tkinter

回答by Frederick Ollinger

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

在 CentOS7 上,为了让它与 Python2 一起工作,我必须这样做:

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.

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

回答by Giovanni G. PY

tkinter comes with python... uninstall python, reinstall it, you're done

tkinter自带python...卸载python,重新安装,大功告成