在 Ubuntu 上的 python3.6 中安装 tkinter

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

Install tkinter in python3.6 on Ubuntu

pythonpython-3.xtkintertclpython-3.6

提问by lapin

+--------+-----------------------------------+
|   OS   |           Ubuntu 12.04            |
+--------+-----------------------------------+
| Python | 2.7, 3.2 and source installed 3.6 |
+--------+-----------------------------------+

Since there are 2 versions of Python 3, anything installed from the repository doesn't work for Python 3.6. The latest version of Python in the repositories is 3.2, so I need source installs or through pip3.6.

由于 Python 3 有 2 个版本,因此从存储库安装的任何内容都不适用于 Python 3.6。存储库中 Python 的最新版本是 3.2,因此我需要安装源代码或通过 pip3.6。

After starting python3.6I tried to import tkinter, which gave the following error. Even though help('modules')returned a list of modules which included tkinter.

开始后python3.6我尝试了import tkinter,这给出了以下错误。即使help('modules')返回了包含 tkinter 的模块列表。

 import tkinter
 ModuleNotFoundError: No module named '_tkinter'

I tried doing the same in python3.2and there were no errors. tkinter._tkintergave the location of tkinter library for python3.2

我尝试在 中做同样的事情python3.2并且没有错误tkinter._tkinter给出了python3.2的tkinter库的位置

I cd'd into the python3.6directory which has all library files and indeed it was missing the tkinter.soobject file.

我 cd 进入python3.6包含所有库文件的目录,并且确实缺少tkinter.so目标文件。

How do I fix the error?

如何修复错误?

I would like to get tkinter/tkagg working as it seems all the modules/ package are already installed.

我想让 tkinter/tkagg 工作,因为似乎所有模块/包都已安装。

After googling some more, I found I need to build python3.6again, but this time with Tcl/Tk options while running configure. I'd rather not. It takes 1hr approx to install python3.6from scratch.

在谷歌搜索更多之后,我发现我需要python3.6再次构建,但这次在运行配置时使用 Tcl/Tk 选项。我宁愿不。python3.6从头开始安装大约需要 1 小时。

Is there some other way where I can tell python3.6where Tcl/Tk is located?

有没有其他方法可以告诉我python3.6Tcl/Tk 的位置?

The problem isn't telling python where tcl/tk is. After messing around with python3.6's source code, and then comparing python3.6 with python3.2, I found out that tkinter calls _tkinter which isn't a python file, it's an .so(shared object) file that python builds during installation via setup.py that uses gcc, that somehow may involve distutils.

问题不在于告诉 python tcl/tk 在哪里。在弄乱了python3.6的源代码,然后将python3.6与python3.2进行比较后,我发现tkinter调用了_tkinter,它不是python文件,它是python在安装过程中构建的.so(共享对象)文件通过使用 gcc 的 setup.py,不知何故可能涉及 distutils。

The new and more appropriate question is how do I build, _tkinter.cpython-36m-i386-linux-gnu.sofrom tcl/tk?

新的更合适的问题是如何_tkinter.cpython-36m-i386-linux-gnu.so从 tcl/tk构建 ?

Note : I do have tcl/tk installed, which I have confirmed using tclsh and wish.

注意:我确实安装了 tcl/tk,我已经使用 tclsh 和希望进行了确认。

回答by Enchant97

run from terminal:

从终端运行:

sudo apt-get install python3.6-tk

or just reinstall completly:

或者只是完全重新安装:

sudo apt-get install python3.6

回答by macm

Python version 3.6.4 (Ubuntu 18.04 LTS)

Python 版本 3.6.4(Ubuntu 18.04 LTS)

sudo add-apt-repository main

sudo add-apt-repository main

sudo apt-get install python3-tk

回答by Billal Begueradj

I faced a similar problem to yours, I am giving the details of it and how I solved it.

我遇到了与您类似的问题,我正在提供它的详细信息以及我如何解决它。

On Ubuntu 16.04 LTS, I have Python 3.5.2 and Python 2.7.12 but I would like to experiment Python3.6 (for various reasons like this one, for example). So I relied on this post:

在 Ubuntu 16.04 LTS 上,我有 Python 3.5.2 和 Python 2.7.12,但我想尝试 Python3.6(例如,出于各种原因,比如这个)。所以我依靠这篇文章

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

When I tried to run a module using tkinter, I got this error message:

当我尝试使用 tkinter 运行模块时,收到以下错误消息:

Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 37, in <module>
    import _tkinter
ModuleNotFoundError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bill.py", line 3, in <module>
    from tkinter import Canvas, Label, Tk, StringVar, Button, LEFT
  File "/usr/lib/python3.6/tkinter/__init__.py", line 39, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

I tried to install tkinter as the message above asks:

我尝试安装 tkinter,因为上面的消息要求:

sudo apt-get install python3-tk
[sudo] password for begueradj: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.5.1-1).
0 upgraded, 0 newly installed, 0 to remove and 8 not upgraded.

Obviously, I still can not use tkinter for Python 3.6. How to fix this problem?

显然,我仍然无法将 tkinter 用于 Python 3.6。如何解决这个问题?

My first blind attempt did not work:

我的第一次盲目尝试没有奏效:

sudo apt-get install python36-tk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python36-tk

The second one works:

第二个有效:

sudo apt-get install python3.6-tk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  tix python3.6-tk-dbg
The following NEW packages will be installed:
  python3.6-tk
0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.
Need to get 74.6 kB of archives.
After this operation, 165 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.6-tk amd64 3.6.5-1+xenial1 [74.6 kB]
Fetched 74.6 kB in 0s (301 kB/s)        
Selecting previously unselected package python3.6-tk:amd64.
(Reading database ... 324106 files and directories currently installed.)
Preparing to unpack .../python3.6-tk_3.6.5-1+xenial1_amd64.deb ...
Unpacking python3.6-tk:amd64 (3.6.5-1+xenial1) ...
Setting up python3.6-tk:amd64 (3.6.5-1+xenial1) ...

And that solved my problem:

这解决了我的问题:

~/python3.6
Python 3.6.5 (default, Mar 29 2018, 03:28:50) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

回答by Swapneneel

Python version 3.6.4 (Ubuntu 18.04 LTS)

Python 版本 3.6.4(Ubuntu 18.04 LTS)

I was having the same error: tkinter module not found. Even after trying to install via pip $ pip install tkinterI got this error, below

我遇到了同样的错误:找不到 tkinter 模块。即使在尝试通过 pip 安装后, $ pip install tkinter我也收到此错误,如下所示

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

I did this tried to install tkinterfor Python3.6 by running the command. It worked for me.$ sudo apt-get install python3.6-tk

我这样做tkinter是为了通过运行命令来安装Python3.6。它对我有用。$ sudo apt-get install python3.6-tk

回答by Agent_T

Try these lines, It might help

试试这些行,它可能会有所帮助

os.environ['TCL_LIBRARY'] = r'C:\Users\asus\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\asus\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'

Recheck path before executing.

执行前重新检查路径。

回答by John

Installed python3.8.2 on ubuntu 20.04, tried importing tkinter but failed. Got this error: da0xxx:~/python_trn/pygui$ python3 -m tkinter /usr/bin/python3: No module named tkinter

在 ubuntu 20.04 上安装了 python3.8.2,尝试导入 tkinter 但失败。得到这个错误:da0xxx:~/python_trn/pygui$ python3 -m tkinter /usr/bin/python3: No module named tkinter

but ran:

但跑了:

da0xxx:~/python_trn/pygui$ sudo apt-get install python3-tk

da0xxx:~/python_trn/pygui$ sudo apt-get install python3-tk

now able to import tkinter!

现在可以导入 tkinter!

da0xxx:~/python_trn/pygui$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

da0xxx:~/python_trn/pygui$ python3 Python 3.8.2(默认,2020 年 4 月 27 日,15:53:34)[GCC 9.3.0] 在 linux 上键入“help”、“copyright”、“credits”或“license”想要查询更多的信息。

import tkinter as tk

将 tkinter 作为 tk 导入

Thanks above!

谢谢楼上!