cooperlkeenan
cooperlkeenan

Reputation: 1

Python Program with Qt 5 Designer not running on boot with crontab -e

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!

Upvotes: 0

Views: 370

Answers (1)

ibrahem
ibrahem

Reputation: 530

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

Upvotes: 1

Related Questions