带有Qt 5 Designer的Python程序未在带有crontab-e的引导下运行



当树莓派使用crontab-e启动时,我正试图运行我的python程序。该代码使用Qt5Designer作为小部件,我认为这就是为什么当我启动它时它没有运行的原因

代码运行正常,但只是当我尝试从启动时运行它时,它不起作用。

这是错误消息:

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
Aborted

这是我的python代码:

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog
from PyQt5.uic import loadUi
import RPi.GPIO as gpio
import time
time.sleep(20)
fan=19
door=26
LED=24
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(fan,gpio.OUT)
gpio.setup(door,gpio.OUT)
gpio.setup(LED,gpio.OUT)

class HMI(QDialog):
def __init__(self):
super(HMI, self).__init__()
loadUi('design.ui',self)

self.setWindowTitle('HMI System')
self.btn.clicked.connect(self.fan_toggle)
self.doorBtn.clicked.connect(self.door_toggle)
self.LEDbtn.clicked.connect(self.LED_toggle)

@pyqtSlot()
def fan_toggle (self):
if gpio.input(fan):
gpio.output(fan,gpio.LOW)
self.btn.setText('Fan')
self.btn.setStyleSheet('QPushButton{ background-color: rgb(60,60,60) ; color: white;}')
else:
gpio.output(fan, gpio.HIGH)
self.btn.setText('Fan')
self.btn.setStyleSheet('QPushButton{ background-color: red; color: white;}')


@pyqtSlot()    
def door_toggle (self):
if gpio.input(door):
gpio.output(door,gpio.LOW)
self.doorBtn.setStyleSheet('QPushButton{ background-color: rgb(60,60,60) ; color: white;}')
self.doorBtn.setText('Door')

else:
gpio.output(door, gpio.HIGH)
self.doorBtn.setStyleSheet('QPushButton{ background-color: red; color: white;}')
self.doorBtn.setText('Opening..')
time.sleep(7)
gpio.output(door,gpio.LOW)
self.doorBtn.setStyleSheet('QPushButton{ background-color: rgb(60,60,60) ; color: white;}')
self.doorBtn.setText('Door')

@pyqtSlot()
def LED_toggle (self):
if gpio.input(LED):
gpio.output(LED,gpio.LOW)
self.LEDbtn.setText('LED')
self.LEDbtn.setStyleSheet('QPushButton{ background-color: rgb(60,60,60) ; color: white;}')
else:
gpio.output(LED, gpio.HIGH)
self.LEDbtn.setText('LED')
self.LEDbtn.setStyleSheet('QPushButton{ background-color: red; color: white;}')






app=QApplication(sys.argv)
widget=HMI()
widget.show()
sys.exit(app.exec_())

我也在远程桌面上做这件事,如果这有什么不同的话?但它也连接到一个小型显示器。

谢谢你的帮助!

在PyQt应用程序可以查看其GUI之前,显示管理器应该首先启动
您可以将(.disktop(文件添加到~/.config/autostart/中,如下所示:

[Desktop Entry]
Comment=BlaBlaBla
Exec=python3 /path/to/python_script.py
Icon=absolute_path_to_icon
Name=MyPyQtApp
Terminal=false
Hidden=false

当系统启动=>时,用户键入密码=>,然后单击"登录":-
应用程序将自动运行

最新更新