Python程序带有QT 5设计师未与crontab -e一起启动启动

发布于 2025-01-21 08:21:28 字数 3037 浏览 3 评论 0原文

当Raspberry Pi使用crontab -e启动时,我正在尝试运行我的Python程序。 该代码将QT5设计器用于小部件,我认为这就是为什么我启动它时没有运行的原因。

该代码正常运行良好,但这只是我尝试从启动运行它的情况下它不起作用。

这是错误消息:

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_())


如果有什么区别,我也在远程桌面上这样做? 但它也连接到一个小型监视器。

谢谢您的任何帮助!

I'm trying to run my python program when the raspberry pi boots up using crontab -e.
The code uses Qt5 Designer for the widgets and I think that's why it's not running when I boot it.

The code runs fine normally but it's just when I try to run it from boot it doesn't work.

Here is the error message:

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

This is my python code:

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_())


I'm also doing this on a remote desktop if that make any difference?
But it also connects to a small monitor.

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2025-01-28 08:21:28

显示管理器应首先在该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

when the system boots => then the user type his password => then clicks "login":-
the App will automatically run

The Display Manager should start first before that PyQt App can view its GUI.
you can add a (.disktop) file into ~/.config/autostart/ like this example:

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

when the system boots => then the user type his password => then clicks "login":-
the App will automatically run

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文