【问题标题】:tkinter in Ubuntu inside Windows 10. Error: "no display name and no $DISPLAY environment variable"tkinter 在 Windows 10 内的 Ubuntu 中。错误:“没有显示名称和没有 $DISPLAY 环境变量”
【发布时间】:2018-06-23 14:01:51
【问题描述】:

我最近安装了适用于 Windows 10 的 Ubuntu 应用,以便我可以将它用于上课。

我正在遵循一些神秘的分配指示来“试用 tkinter”。

我按照位于 here: How to install and use Tkinter in ubuntu / Debian / Linux mint 的说明在 Ubuntu 终端窗口中安装了包 python3-tk。

然后我执行

~$ python3

结果

Python 3.5.2 (default, Aug 18 2017, 17:48:00)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

然后是以下命令:

>>> import tkinter
>>> tkinter.Tk()

结果错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, 
wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/questions/19309085/…
  • 情况不同,因为我没有使用 ssh。一切都在我自己的机器上。我更新了我的帖子以更好地澄清。可能会从该帖子中获得一些见解(已阅读多个相似的内容),但我不知道如何将这些帖子中的信息应用于我的情况。
  • 好吧,冒着问显而易见的风险,您是否有一个名为 $DISPLAY 的环境变量?
  • 当 tcl/tk for linux 尝试连接到 X 服务器时出现错误消息。我的猜测是你必须告诉 Ubuntu 这样做,或者告诉 tcl/tk 不要寻找一个。无论哪种方式我都不知道。 (前面问题的解决方案是告诉 matplotlib 不要寻找 X 服务器。)多说一些你实际做了什么。您应该直接运行 Python,而不是通过 IDLE。也许你做了前者,尽管有标签,因为我怀疑当 it 调用 tkinter.Tk() 时 IDLE 应该失败。
  • 我使用 Linux 并且拥有DISPLAY=:0。我可以在命令行DISPLAY=:0export DISPLAY=:0 中更改它。或者我可以把它放在文件.bashrc 中,每次我登录时都会执行 - 所以我不必手动执行。

标签: ubuntu tkinter windows-10 xserver xming


【解决方案1】:

已解决:

注意:这是在 Windows 10 中使用 Ubuntu 应用程序时

安装Xming X server for Windows

在安装过程中,有一个选项可以将显示设置为 0。

按照建议将export DISPLAY=:0; 行添加到.bashrc

【讨论】:

  • 在运行 gitk 命令时出现问题。现在工作正常感谢您的回答
【解决方案2】:

我遇到了同样的问题。我最近从商店安装了“Windows 上的 Ubuntu 上的 Bash”应用程序,运行尝试创建 python UI 的 python 脚本时出现以下错误:

_tkinter.TclError: no display name and no $DISPLAY environment variable

我按照@Zach 的说明进行操作,但我的问题仍然存在。这就是我在关注@Zach 后解决问题的方式。

  1. 我首先安装了 Xming X Server for Windows。
  2. 然后我在 Ubuntu 终端上进入我的主目录 ~,并通过执行 vim .bashrc 打开我的 .bashrc 文件。
  3. 在文件末尾,我添加了以下行:export DISPLAY=:0;。我没有使用“执行”,而是使用了“导出”。

确保在尝试执行 python 脚本之前关闭所有终端实例。

这对我有用!

【讨论】:

  • 是的。我不能打字。或者显然,不知道“导出”和“执行”之间的区别。 Drrrrr。
【解决方案3】:

如果你不想安装Xming X Server For Windows

然后在windows中使用CMD就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2016-10-02
    • 2015-05-08
    • 2018-09-03
    • 2019-11-03
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多