Search code examples
pythonpython-3.xpyqtraspberry-pipyqt5

PyQt: How to run GUI on Raspberry Pi desktop startup?


Dear Stackoverflow community,

I am struggling with running a python script that executes a PyQt5 GUI on desktop startup of Raspberry Pi 3B with Raspbian Jessie.

What do I have so far?

  • Python script with shebang #!/usr/bin/env python3 in first line (python3 --version is 3.4.2) running the GUI without any problems

  • Shell script (.sh) that is able to execute the GUI with the following lines:

    #!/bin/bash python3 GUI.py

Information that may help:

  • If I place both files in the same directory somewhere, the Shell script starts the GUI, but if they are on the desktop, it doesn't.

  • Automatic login to desktop is enabled.

Thank you in advance for any help.

RaspiManu

UPDATE:

I solved my Problem with a lot of testing and posted an answer for other users.


Solution

  • After a lot of testing, I figured it out myself. Here's how it worked for me...

    Create autorun-file:

    2.1 LXTerminal: cd /home/pi/.config/autostart

    2.2 LXTerminal: sudo nano pythonscript.desktop

    2.3 pythonscript.desktop:

    [Desktop Entry]
    Version=1.0
    Name=YourName
    Comment=Your comment
    Exec=/home/pi/pythonscript.py -nograb #-nograb for comboBox on touch Screen
    Icon=/usr/share/pixmaps/python.xpm
    Path=/home/pi/
    Terminal=false
    StartupNotify=true
    Type=Application
    Categories=Utility;Application;
    

    2.4 Ctrl+O, Ctrl+X, sudo reboot

    Good to know:

    It is important, that you can't use just any path to your script. The script has to be directly in the /home/pi/ directory, so you would use Exec=/home/pi/pythonscript.py in the autorun-file (.desktop). I also learned, that if your script loads for example an image with PIL, this image has to be somewhere else, maybe on your desktop, because it can't be opened out of the /home/pi/ directory.

    If your GUI has a comboBox and you are using a touch screen, the comboBox might make your whole GUI unuseable after you touched it. Using Exec=/home/pi/pythonscript.py -nograb solves this problem.

    StartupNotify=true is important for starting GUI scripts.

    Hope this helps,

    RaspiManu