Python 无法导入 tkinter(或 Tkinter)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39600159/
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-19 22:30:11  来源:igfitidea点击:

Can't import tkinter (or Tkinter)

pythonimporttkinter

提问by Ian Cole

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

我正在尝试使用 Python 2.7 将 Tkinter 导入我的项目,但出现错误:

ImportError: No module named tkinter

导入错误:没有名为 tkinter 的模块

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

在有人说之前,我已经尝试过“Tkinter”和“tkinter”,但得到了完全相同的信息。

回答by Preston Hager

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.

如果这不起作用,请尝试重新安装 tkinter。如果您不知道如何重新安装 tkinter,请查看此处的 tkinter 安装页面

回答by Shardul Nalegave

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

如果您使用的是 Ubuntu 或 Debian 操作系统,请尝试以下操作:-

sudo apt-get install python-tk

Or if you are using Python 3:-

或者,如果您使用的是 Python 3:-

sudo apt-get install python3-tk

回答by BE Official

if you install Python 3.7 you will need to type this:

如果你安装 Python 3.7,你需要输入:

from tkinter import *

then you can use tkinter.

那么你可以使用tkinter。

回答by Ron Lauterbach

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:

一些编译器预装了 tkinter。例如,如果您使用 IDLE tkinter 是预装的。据我所知,如果您使用 IDLE,则必须单击一个框才能安装 tkinter。如果您没有使用 IDLE,请检查 tkinter/Tkinter 是否包含在您的 Site Packages 文件夹中。考虑重新安装您正在使用的编译器/解释器。确定安装后,您必须使用的语法取决于您使用的 Python 版本。我不太确定 Python 2,但我认为你写:

import Tkinter

For Python 3 you write:

对于 Python 3,您编写:

import tkinter

or the more often used:

或者更常用的:

from tkinter import * 

回答by viren parmar

You should install the tkinterpackage for python. Tkinterhas been renamed to tkinterin Python 3. The 2to3tool will automatically adapt imports when converting your sources to Python 3.

你应该安装tkinterpython的包。在 Python 3 中,Tkinter已重命名为tkinter。该2to3工具将在将您的源代码转换为 Python 3 时自动调整导入。

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

注意:以下命令假设您运行的是 Linux 设备。

For Python 2:

对于 Python 2:

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

for Python 3:

对于 Python 3:

pip install python3-tk
sudo apt-get install python3-tk

回答by viren parmar

Had the same problem ,please try this;

有同样的问题,请试试这个;

from Tkinter import *