在连接显示器的无头linux上运行GUI

2024-04-19 19:35:14 发布

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

我正在研制一个RPI zero W,带有一个连接的7英寸显示器。我想用Tkinter for Python为它开发一个GUI应用程序。我想把它用在我自己开发的PC统计监视器上,所以我想要一个小的占地面积,我认为使用更好的PI是一种过度使用,并且会破坏它的用途(但如果需要的话,我愿意改变它)

我正在运行一个无头树莓操作系统,我可以看到终端显示在显示器上,现在我只想在终端上覆盖GUI应用程序。我正在运行headless,以加快启动速度并降低CPU上的资源成本

#!/usr/bin/env python3

from tkinter import *
import sys
import os

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

root = Tk()


#Creating a Label Widget
myLabel = Label(root, text="Hello World!")
#Shoving it onto screen
myLabel.pack()

root.mainloop()

启动应用程序时,我得到一个回报:

no display found. Using :0.0
Traceback (most recent call last):
  File "./gui_test.py", line 11, in <module>
    root = Tk()
  File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"

我怎样才能解决这个问题


Tags: noimport应用程序终端ostkinterusrdisplay