PyQt:如何在Raspberry Pi桌面启动上运行GUI?

时间:2018-05-16 09:59:37

标签: python python-3.x pyqt raspberry-pi pyqt5

亲爱的Stackoverflow社区,

我正在努力使用运行一个python脚本,该脚本使用Raspbian Jessie在Raspberry Pi 3B的桌面启动上执行一个PyQt5 GUI。

到目前为止我有什么作用?

  • 第一行中使用shebang #!/usr/bin/env python3的Python脚本(python3 --version是3.4.2)运行GUI没有任何问题

  • 能够使用以下行执行GUI的Shell脚本(.sh):

    #!/bin/bash python3 GUI.py

可能有用的信息:

  • 如果我将两个文件放在同一个目录中,那么Shell脚本会启动GUI,但如果它们在桌面上,则不会。

  • 启用自动登录桌面。

提前感谢您的帮助。

RaspiManu

更新

我通过大量测试解决了我的问题并为其他用户发布了答案。

2 个答案:

答案 0 :(得分:1)

经过大量测试后,我自己弄清楚了。以下是它对我有用的方法......

创建自动运行文件:

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

很高兴知道:

重要的是,您不能只使用脚本的任何路径。该脚本必须直接位于/home/pi/目录中,因此您可以在自动运行文件(.desktop)中使用Exec=/home/pi/pythonscript.py。我还了解到,如果您的脚本加载例如带有PIL的图像,则该图像必须位于其他位置,可能在您的桌面上,因为它无法在/home/pi/目录中打开。

如果您的GUI有一个comboBox并且您正在使用触摸屏,则comboBox可能会在您触摸它后使您的整个GUI无法使用。使用Exec=/home/pi/pythonscript.py -nograb解决了这个问题。

StartupNotify=true对于启动GUI脚本非常重要。

希望这有帮助,

RaspiManu

答案 1 :(得分:0)

您可以通过以下链接创建在启动时运行的后台服务 Service method

并附加此行

service yourdaemon start
/etc/rc.local中的

假设您的服务名称是'yourdaemon'

警告:使用root previleges

示例服务文件

#! /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

:

在/etc/init.d/中以'yourdaemon'的名称保存,并使用

使其可执行
chmod +x yourdaemon