首页 > 解决方案 > 在连接了显示器的无头 linux 上运行 GUI

问题描述

我正在开发一个连接 7 英寸显示器的 RPI 零 W。我想用 Tkinter for Python 为它开发一个 GUI 应用程序。我想将它用于我自己开发的 PC 统计监视器,所以我想要一个小的占用空间,我认为像使用更好的 PI 那样使用会是一种矫枉过正并且会破坏目的(但如果需要,我愿意改变它) )。

我正在运行一个无头 Raspberry OS,我可以看到显示器本身显示的终端,现在我只想将 GUI 应用程序覆盖在终端上。我正在无头运行以加快启动速度并降低 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"

我怎样才能解决这个问题?

标签: pythonlinuxtkinterraspberry-pioperating-system

解决方案


推荐阅读