问题
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 python3in first line (- python3 --versionis 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.
回答1:
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
回答2:
You can make a background service that runs on startup, by following this link Service method
and By appending this line
service yourdaemon start
in /etc/rc.local
assuming your service name is 'yourdaemon'
Caution: Use the root previleges
Example service file
#! /bin/sh
### BEGIN INIT INFO
# Provides:          yourdaemon
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Your Daemon
# Description:       Your Daemon
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Your Daemon"
NAME=yourdaemon
DAEMON=/hannext/yourdaemon.py  # Path to your python file
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
LOGFILE=/var/log/snc/$NAME.log
. /lib/lsb/init-functions
do_start()
{
        echo "$(date +%F) $(date +%T) DAEMON   : Starting $DESC service" >> $LOGFILE
        start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON --make-pidfile --background
}
do_stop()
{
        echo "$(date +%F) $(date +%T) DAEMON   : Stopping $DESC service" >> $LOGFILE
        start-stop-daemon --stop $DAEMON --quiet --oknodo --pidfile $PIDFILE
        rm -f $PIDFILE
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
        #
        # If the daemon can reload its configuration without
        # restarting (for example, when it is sent a SIGHUP),
        # then implement that here.
        #
        start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
        return 0
}
case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
  #reload|force-reload)
        #
        # If do_reload() is not implemented then leave this commented out
        # and leave 'force-reload' as an alias for 'restart'.
        #
        #log_daemon_msg "Reloading $DESC" "$NAME"
        #do_reload
        #log_end_msg $?
        #;;
  restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac
:
save it by the name of 'yourdaemon' in /etc/init.d/ and make it executable using
chmod +x yourdaemon
来源:https://stackoverflow.com/questions/50367837/pyqt-how-to-run-gui-on-raspberry-pi-desktop-startup