【问题标题】:can not connect to Xserver while running python file using pyQT4 via ubuntu service在ubuntu服务中使用pyQT4运行python文件时无法连接到服务器
【发布时间】:2021-01-05 06:44:57
【问题描述】:

我正在尝试执行使用 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()

【问题讨论】:

  • python脚本是做什么的?它真的需要图形服务器(X11)来运行吗?你能给我们提供它的代码,或者至少是一个minimal, reproducible example吗?
  • 是的,我已将脚本添加到我的问题@musicamante

标签: linux pyqt4 ubuntu-18.04 xserver


【解决方案1】:

通常服务与能够登录/启动 X 环境的普通用户没有相同的环境。 因此我猜DISPLAY 没有设置。 在您的服务文件中尝试此操作,但确保它会在 X 已经运行后启动...

ExecStart = env -i DISPLAY=:0.0 /usr/bin/python3 /home/nvidia/main

示例 - 用户 root 尝试在 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 中检查这一点。

【讨论】:

  • ExecStart = env -i DISPLAY=:0.0 /usr/bin/python3 /home/nvidia/main 修改此行会导致 Exec 格式错误,Xserver 已经为 root 运行
  • Mh,所以在 X 服务未运行的情况下尝试一下 - 是否有任何情况下脚本(不是服务)运行时没有错误? - 我用init 2(不带X 的单用户)和init 5(带X 的多用户)更改运行或不运行
  • 是的,主 qt 窗口指令之前的所有情况都在运行。我检查了有和没有 X 的单用户以及有和没有 X 的多用户
  • 我也试过禁用 xhost 的访问控制
【解决方案2】:
[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 服务成功执行了 GUI

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2012-09-10
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    相关资源
    最近更新 更多