在Ubuntu上的python3.6中安装tkinter

时间:2017-03-06 10:22:05

标签: python python-3.x tkinter tcl python-3.6

python3.6

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

开始import tkinter后,我尝试help('modules'),这会产生以下错误。即使 import tkinter ModuleNotFoundError: No module named '_tkinter' 返回了包含tkinter的模块列表。

python3.2

我尝试在tkinter._tkinter中执行相同操作,并且没有错误python3.6给出了python3.2的tkinter库的位置

我进入tkinter.so目录,其中包含所有库文件,实际上它缺少python3.6目标文件。

如何修复错误?

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

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

还有其他方法我可以告诉_tkinter.cpython-36m-i386-linux-gnu.so Tcl / Tk在哪里吗?

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

新的更合适的问题是如何构建, 来自tcl / tk的{{1}}?

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

6 个答案:

答案 0 :(得分:10)

从终端运行:

sudo apt-get install python3.6-tk

或者只是重新安装completly:

sudo apt-get install python3.6

答案 1 :(得分:4)

我遇到了类似的问题,我正在详细介绍它以及我是如何解决它的。

在Ubuntu 16.04 LTS上,我有Python 3.5.2和Python 2.7.12,但我想试验Python3.6(例如this one之类的各种原因)。所以我依靠这个post

return [
    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'album' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\AlbumController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

    'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

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

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

我尝试按照上面的消息安装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

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

我的第一次失明尝试不起作用:

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.

第二个有效:

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

这解决了我的问题:

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

答案 2 :(得分:3)

Python版本3.6.4(Ubuntu 18.04 LTS)

sudo add-apt-repository main

sudo apt-get install python3-tk

答案 3 :(得分:1)

在ubuntu 20.04上安装了python3.8.2,尝试导入tkinter但失败。得到这个错误: da0xxx:〜/ python_trn / pygui $ python3 -m tkinter / usr / bin / python3:没有名为tkinter的模块

但跑了:

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

现在可以导入tkinter!

da0xxx:〜/ python_trn / pygui $ python3 Python 3.8.2(默认,2020年4月27日,15:53:34) Linux上的[GCC 9.3.0] 输入“帮助”,“版权”,“信用”或“许可证”以获取更多信息。

将tkinter导入为tk

谢谢!

答案 4 :(得分:0)

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

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'

执行前重新检查路径。

答案 5 :(得分:0)

Python版本3.6.4(Ubuntu 18.04 LTS)

我遇到了同样的错误:找不到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

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