TkInter 错误:没有显示名称和 $DISPLAY 环境变量 [英] TkInter error: no display name and no $DISPLAY environment variable

查看:54
本文介绍了TkInter 错误:没有显示名称和 $DISPLAY 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Repl.it 上开发 TkInter,但遇到了问题,这是我的代码:

I'm working on TkInter on Repl.it and have run into a problem, this is my code:

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry('400x400')

我遇到了这个错误:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    root = tk.Tk()
  File "/usr/local/lib/python3.7/tkinter/__init__.py", line 202
3, in __init__
    self.tk = _tkinter.create(screenName, baseName, className,
interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment
variable

我该如何解决这个问题?

How do I solve this?

推荐答案

您显然是在尝试使用 repl.it 的Python"来做到这一点,它不支持 tkinter 需要的显示.他们确实提供了一个单独的Tkinter"选项,尽管它在语言列表中很远.这是一个快捷方式:https://repl.it/languages/tkinter

You're apparently trying to do this with repl.it's "Python", which doesn't support the display that tkinter needs. They do offer a separate "Tkinter" option, although it's quite far down the list of languages. Here's a shortcut: https://repl.it/languages/tkinter

你不会得到那个错误.为了实际显示窗口,您还必须在当前代码下添加:

There you don't get that error. In order to actually get the window shown, you'll have to also add this under your current code:

root.mainloop()

演示

这篇关于TkInter 错误:没有显示名称和 $DISPLAY 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆