Python程序带有QT 5设计师未与crontab -e一起启动启动
当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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示管理器应首先在该PYQT应用程序可以查看其GUI之前开始。
您可以将(.diskTop)文件添加到
〜/.config/autostart/
之类的示例中: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:when the system boots => then the user type his password => then clicks "login":-
the App will automatically run