python - 没有 $DISPLAY 环境变量
问题描述
我正在尝试在我的树莓派屏幕上运行 python tkinter GUI。现在,我在使用的树莓派上只有一个 HDMI 端口,那是我的屏幕。每当我运行代码时,都会出现错误
_tkinter.TclError: no display name and no $DISPLAY environment variable
请注意,我在笔记本电脑的 ssh 终端上执行该命令。
我已经查看了这个问题的先前答案,例如本地机器 _tkinter.TclError: could't connect to display ":0" _tkinter.TclError: no display name and no $DISPLAY environment variable
但似乎没有人能解决这个问题。
Tkinter 图形用户界面
#!/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()
解决方案
推荐阅读
- python - 用 scipy 拟合具有固定均值的伽马分布?
- python - 在 TensorFlow 中实现 ARIMA(X) 模型
- jboss - 将 .wars 从 JBOSS 7.1 迁移到 JBOSS 7.2 后为交换 HttpServerExchange 设置错误代码 500
- angular - 生产后的角度不加载svg
- sql-server - Python 参数化查询并插入 SQL Server
- c++ - ofstream 以二进制模式在文件上写入空字符
- flutter - 颤振动画列表视图元素不起作用
- excel - 如何在excel的索引函数中增加行值?
- python - 如何在sql查询中使用整数变量
- java - 我正在尝试制作一个在命令行上玩的跳棋游戏,我不知道如何根据用户输入移动棋子