我正在尝试执行使用PyQT4的python文件
我正在运行下面的服务文件
[Unit]
Description = Test
After=multi-user.target
[Service]
Type=simple
ExecStart = /usr/bin/python3 /home/nvidia/main
Restart=on-abort
[Install]
WantedBy =multi-user.target此文件位于/lib/systemd/system/test.service下,我将通过systemctl start Test启动此服务
但启动此服务会导致标记为cannot connect to X Server, failed with result exit-code的错误
我正在使用脚本
#!/usr/bin/python
#################################################################################################################################################
# Author = Rucha
# Version = V 2.0.3
# Class = PR01 OOP
# Module = pyqt4
# Date = Jan 02 2021
#################################################################################################################################################
import sys
from PyQt4 import QtGui
######################################################################################################################################
class MainWindow:
def __init__(self):
self.vbox = QtGui.QHBoxLayout()
def Title(self,Window,Name):
Window.setWindowTitle(Name)
def window(self):
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.setGeometry(800,800,500,500)
self.Title(w,"Test")
w.show()
sys.exit(app.exec_())
MainWindow1 = MainWindow()
MainWindow1.window()发布于 2021-01-26 21:24:05
[Unit]
Description = Test
After=multi-user.target
[Service]
Type=simple
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/nvidia/.Xauthority"
ExecStart = /usr/bin/python /home/nvidia/main
Restart=on-failure
[Install]
WantedBy =graphical.target我使用上面的指令值通过systemd服务成功地执行了图形用户界面
发布于 2021-01-05 17:57:08
通常,服务的环境与能够登录/启动X环境的普通用户的环境不同。因此,我猜DISPLAY没有被设置。在您的服务文件中尝试此操作,但请确保它将在X运行后启动...
ExecStart = env -i DISPLAY=:0.0 /usr/bin/python3 /home/nvidia/main示例-用户根用户尝试在X上运行一些东西-使用和不使用DISPLAY
#kcalc
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.
#env -i DISPLAY=:0.0 kcalc
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'您可以在带有和/或不带有DISPLAY变量的XTerm中检查这一点。
https://stackoverflow.com/questions/65574289
复制相似问题