【问题标题】:How to get tkinter working with Ubuntu's default Python 2.7 install?如何让 tkinter 使用 Ubuntu 的默认 Python 2.7 安装?
【发布时间】:2012-07-29 22:21:35
【问题描述】:

我使用的是 Ubuntu 11.10,它预装了 Python 2.7.3。

我通过 apt-get install 安装了 python3 包和 python3-tk 包,它们“开箱即用”一起工作。

但是虽然我已经安装了 python-tk 包,但我不知道如何让 Python2.7 看到它。我尝试导入时收到以下错误消息。

import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

编辑:我还根据Python-tk package not recognized in Python 2.7.3 运行了以下内容,得到:

$ file /usr/lib/libtk8.5.so.0
/usr/lib/libtk8.5.so.0: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped

$ uname -a
Linux bugbot 3.0.0-23-generic-pae #39-Ubuntu SMP Thu Jul 19 19:39:19 UTC 2012 i686 i686 i386 GNU/Linux

【问题讨论】:

  • 我知道在 Python 3 中,Tkinter 模块是小写的(“tkinter”)。所以你可以尝试“import tkinter”。话又说回来,这个人(ubuntuforums.org/showthread.php?t=1699456)似乎也有类似的问题,但看起来它比仅仅使用错误的名字更深入。
  • 我已经尝试过小写和大写...都没有工作,只有大写让我得到更具体的错误消息。

标签: python ubuntu tkinter


【解决方案1】:

我在这个问题上花费了太多时间后才弄清楚,所以希望我可以为其他人省去麻烦。

我发现这个旧的错误报告被认为是无效的,它提到了我遇到的确切问题,我有 Tkinter.py,但它找不到模块 _tkinter:http://bugs.python.org/issue8555

我使用 apt-get 安装了 tk-dev 包,并在 Python2.7.3 目录中使用 ./configure、make 和 make install 重新构建了 Python。现在我的 Python2.7 可以导入 Tkinter,耶!

我有点恼火的是,Python 安装文档中根本没有提到 tk-dev 包……如果像我一样,有人应该发现它们是 Python 中缺少的模块,下面是另一个有用的资源缺少超过 _tkinter。

Building Python and more on missing modules

【讨论】:

  • 如果你在fedora下编译python,在./configure之前做sudo yum install tk-devel
【解决方案2】:

我在 python3.4 上遇到了同样的问题(ImportError: No module named '_tkinter')

sudo apt-get install tk-dev

进入python源码目录

./configure
make
sudo make install

【讨论】:

    【解决方案3】:

    以下解决了我在 Linux Mint 16 上的问题:

    sudo apt-get install tk-dev python-tk 
    

    我使用virtualenv但不想编译python,为了让python支持tk,需要安装python-tk。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2014-11-09
      • 2011-05-02
      相关资源
      最近更新 更多