Python $VScode中Tkinter的显示环境问题

Python $VScode中Tkinter的显示环境问题,python,tkinter,visual-studio-code,display,Python,Tkinter,Visual Studio Code,Display,我正在尝试使用tkinter构建一个简单的倒计时计时器,但是当我运行代码时,会出现以下消息: File "/home/user/Desktop/Beginner Projects/Pythons/countdown timer.py", line 20, in <module> root = Tk() File "/usr/lib/python3.8/tkinter/__init__.py", line 2261, in __ini

我正在尝试使用tkinter构建一个简单的倒计时计时器,但是当我运行代码时,会出现以下消息:

File "/home/user/Desktop/Beginner Projects/Pythons/countdown timer.py", line 20, in <module>
    root = Tk()
  File "/usr/lib/python3.8/tkinter/__init__.py", line 2261, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

我在使用PySimpleGUI的Ubuntu20.04.1上也遇到了同样的问题

如果通过单击右上角的绿色箭头(对应于“在终端中运行Python文件”)尝试运行脚本时出现问题,一种解决方案是进入
终端
,并适当设置显示变量。
在我的例子中,以下命令起到了作用:
导出显示=:1
DISPLAY变量的实际值取决于环境的具体情况(例如,请参见[https://unix.stackexchange.com/a/612453)

如果在按F5(调试)或Ctrl+F5(不调试运行)时出现问题,则需要编辑
launch.json
文件。 可通过运行->打开配置菜单访问此文件。 (这假定已选择调试/运行的配置。) 我们需要指示VSCode设置显示环境变量。为此,请在适当的部分添加行
“env”:{“DISPLAY”::1”},
。 在我的例子中,该部分是以
“name:”Python:Current File“,
开头的部分


我想这通常是因为它不能在屏幕上显示GUI,但我不知道为什么VS代码会出现这种情况,我使用VS代码,它很好。请发布你的代码。我需要你的代码来检查它。代码在我的系统上运行得非常好
from tkinter import *
from tkinter import ttk
from tkinter import font
import time
import datetime

global endTime

def quit(*args):
    root.destroy()

def cant_wait():
    timeLeft = endTime - datetime.datetime.now()
    timeLeft = timeLeft - datetime.timedelta(microseconds=timeLeft.microseconds)

    txt.set(timeLeft)

    root.after(1000, cant_wait)

root = Tk()
root.attributes("-fullscreen",False)
root.configure(background="black")
root.bind("x", quit)
root.after(1000, cant_wait)

endTime = datetime.datetime(2020,6,7,8,0,0)

fnt = font.Font(family="Helvetica", size=90, weight="bold")
txt = StringVar()
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground="white", background="black")
lbl.place(relx = 0.5, rely = 0.5, anchor="center")

root.mainloop()