没有$DISPLAY环境变量

2024-04-25 21:35:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的raspberry pi屏幕上运行python tkinter GUI。现在,我在使用的raspberry pi上只有一个HDMI端口,那就是我的屏幕。每当我运行代码时,都会出现一个错误

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

请注意,我在笔记本电脑的ssh终端上执行该命令

我已经看过这个问题以前的答案,例如local machine _tkinter.TclError: couldn't connect to display ":0"_tkinter.TclError: no display name and no $DISPLAY environment variable

但似乎没有人能解决这个问题

Tkinter GUI

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("640x480")

#Define Canvas
canvas = tk.Canvas(root, width=1920, height=1080)
canvas.grid(row=0,column=0)



# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

# Called when user presses View Log button
def viewLogRaise():
    #Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    quote = """HAMLET: To be, or not to be--that is the question:
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune
    Or to take arms against a sea of troubles
    And by opposing end them. To die, to sleep--
    No more--and by a sleep to say we end
    The heartache, and the thousand natural shocks
    That flesh is heir to. 'Tis a consummation
    Devoutly to be wished."""
    logText.insert(tk.END, quote)

def backToMenu():
    #Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")

# Background
pathToGif = "redpoly4.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root,fg="white", text="FUZE",borderwidth=2,relief="solid", bg=_from_rgb((239, 36, 37)), font=("Courier", 100))
titleLabelWindow = canvas.create_window(320,120,window=titleLabel)
logButton = tk.Button(root,fg="white",text="View Log",command=viewLogRaise,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)), font=("Courier", 46))
logButtonWindow = canvas.create_window(320,350,window=logButton)
backButton = tk.Button(root,fg="white",text="Back",command=backToMenu,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)),font=("Courier", 20))
backButtonWindow = canvas.create_window(70,440,window=backButton)
canvas.itemconfigure(backButtonWindow, state="hidden")
logText=tk.Text(root,bg="white",height=22,width=55,borderwidth=2,relief="solid")
logTextWindow = canvas.create_window(350,220,window=logText)
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()

Tags: andtonofromtkintercreatergbroot