Python 如何使用 Pycharm 安装 tkinter?

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

how to install tkinter with Pycharm?

pythontkinterpycharm

提问by TheProgramMAN123

I used sudo apt-get install python3.6-tkand it works fine. Tkinter works if I open python in terminal, but I cannot get it installed on my Pycharm project. pip installcommand says it cannot find Tkinter. I cannot find python-tk in the list of possible installs either.

我用过sudo apt-get install python3.6-tk,效果很好。如果我在终端中打开 python,Tkinter 可以工作,但我无法将它安装在我的 Pycharm 项目中。pip install命令说它找不到 Tkinter。我也无法在可能的安装列表中找到 python-tk。

Is there a way to get Tkinter just standard into every virtualenv when I make a new project in Pycharm?

当我在 Pycharm 中创建一个新项目时,有没有办法让 Tkinter 成为每个 virtualenv 的标准?

Edit: on Linux Mint

编辑:在 Linux Mint 上

Edit2: It is a clear problem of Pycharm not getting tkinter guys. If I run my local python file from terminal it works fine. Just that for some reason Pycharm cannot find anything tkinter related.

Edit2:Pycharm 没有得到 tkinter 人是一个明显的问题。如果我从终端运行我的本地 python 文件,它工作正常。只是出于某种原因,Pycharm 找不到任何与 tkinter 相关的内容。

回答by Mattatat-tat

Make sure you use the right import statement for your version of Python.

确保为您的 Python 版本使用正确的 import 语句。

Python 2.7

蟒蛇 2.7

from Tkinter import *

For Python 3.x

对于 Python 3.x

from tkinter import *

回答by Sam

Python already has tkinter installed. It is a base module, like random or time, therefore you don't need to install it.

Python 已经安装了 tkinter。它是一个基本模块,如随机或时间,因此您不需要安装它。

回答by Dinko Pehar

For python 2use:

对于python 2使用:

sudo apt-get install python-tk

For python 3use:

对于python 3使用:

sudo apt-get install python3-tk

When you display info about packages it states:

当您显示有关包的信息时,它会指出:

Tkinter - Writing Tk applications with Python2 (or Python 3.x)

Tkinter - 使用 Python2(或 Python 3.x)编写 Tk 应用程序



But my assumption is that PyCharm created it's own virtualenv for you project, so you are probably using wrong python interpreter in PyCharm.

但我的假设是 PyCharm 为您的项目创建了自己的 virtualenv,因此您可能在 PyCharm 中使用了错误的 Python 解释器。

Open your PyCharm project. Go to File->Settings->Project->Project Interpreter. At top, you will see what python interpreter is PyCharm using for a current project. If that's not the system one you have, find path to system interpreter and add it to Python Interpretersin PyCharm.

打开您的 PyCharm 项目。去File->Settings->Project->Project Interpreter。在顶部,您将看到 PyCharm 用于当前项目的 Python 解释器。如果这不是您拥有的系统,请找到系统解释器的路径并将其添加到Python InterpretersPyCharm 中。

More details on PyCharm Documentation.

有关PyCharm 文档的更多详细信息。

回答by Tharindu Kumara

Pycharm comes with tkinter by default.

Pycharm 默认带有 tkinter。

Basically you have to import it

基本上你必须导入它

from tkinter import *

top = Tk()
# Code to add widgets will go here...
top.mainloop()