Python:在Linux上的virtualenv上安装Tkinter

gar*_*may 5 tkinter virtualenv python-2.7

我在Linux机器上使用virtualenv.我没有sudo访问权限,所以只能使用pip.

使用以下命令:

 pip install python-tk
Run Code Online (Sandbox Code Playgroud)

但这导致了错误

Collecting python-tk
  Could not find a version that satisfies the requirement python-tk (from versions: )
No matching distribution found for python-tk
Run Code Online (Sandbox Code Playgroud)

Bil*_*adj 2

您无法使用 pip 安装 tkinter,因为 tkinter 是名为 Tk 的 C++ 库的接口,而 pip 是使用 Python 编码的。

幸运的是,您不必担心上述声明,因为 tkinter 作为标准 Python 发行版的内置库。

所以你要做的是:

  1. 转到您的 virtualenv 目录:cd to_your_virtualenv_directory
  2. 激活它:source bin/activate
  3. 访问其中的 python shell:python
  4. 然后import tkinter as tk

笔记:

根据您的设置,也许当您键入时python您会注意到系统会提示您使用 Python 2.x。在这种情况下,只需输入:import Tkinter as Tk。但是,如果键入python导致您使用 Python 3.x(正如我在我的计算机上设置的那样),但您更喜欢使用 Python 2.x,则只需键入python2而不是python.