Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt:如何在Raspberry Pi桌面启动上运行GUI?_Python_Python 3.x_Pyqt_Raspberry Pi_Pyqt5 - Fatal编程技术网

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

Python PyQt:如何在Raspberry Pi桌面启动上运行GUI?,python,python-3.x,pyqt,raspberry-pi,pyqt5,Python,Python 3.x,Pyqt,Raspberry Pi,Pyqt5,亲爱的社区: 我正在努力运行一个python脚本,该脚本在桌面启动时执行一个pyqt5gui 到目前为止我有什么经验? 带shebang#的Python脚本/usr/bin/env python3在第一行(python3--versionis 3.4.2)运行GUI时没有任何问题 Shell脚本(.sh),能够使用以下行执行GUI: #/bin/bash python3 GUI.py 可能有帮助的信息: 如果我将两个文件放在同一个目录中的某个地方,Shell脚本将启动GUI,但如果它们位于

亲爱的社区:

我正在努力运行一个python脚本,该脚本在桌面启动时执行一个pyqt5gui

到目前为止我有什么经验?

  • 带shebang
    #的Python脚本/usr/bin/env python3
    在第一行(
    python3--version
    is 3.4.2)运行GUI时没有任何问题

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

    #/bin/bash
    python3 GUI.py

可能有帮助的信息:

  • 如果我将两个文件放在同一个目录中的某个地方,Shell脚本将启动GUI,但如果它们位于桌面上,则不会启动

  • 已启用自动登录到桌面

提前感谢您的帮助

拉斯皮马努

更新:


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

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

加上这一行

service yourdaemon start
in/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/中以“yourdemon”的名称保存它,并使用

chmod +x yourdaemon

经过多次测试,我自己找到了答案。这就是它对我的作用

创建自动运行文件:

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

2.2 LX终端:
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重新启动

好消息:

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

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

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

希望这有帮助


拉斯皮马努

非常感谢您的回答,请原谅我的问题。。。我对此非常陌生,从未使用过守护进程。我知道,你可以使用<代码> /ETC/init,d/c>中的骨架文件来构建一个,但是我查看了一些像SUDO或重新启动的守护进程,它们似乎是用C++或C语言编写的,我不能使用。你能再解释一下创建守护进程的步骤吗?我如何使用root previleges呢?我已经编辑了我的答案,并加入了一个示例脚本来制作一个服务,你绝对让我受益匪浅,因为你帮了我这么多。非常感谢。我现在不工作了,但我明天一定会试试并告诉你。如果是这样的话,
$NAME
应该保持原样并使用
NAME=yourdemon
,还是我必须更改
yourdemon
和.py文件路径以外的内容?这是服务的名称,所以你应该相应地命名它。在测试之后,如果您觉得它有帮助,请将其标记为正确。我将遵循您的答案,并能够在启动时运行pyqt5应用程序。但它并没有加载图像。我的项目目录位于包含所有图像的
/home/pi/Documents/project
中。我是否需要根据您的建议将我的项目移动到/hom/pi/亲爱的@SAndrew,这是很久以前的事了,我不完全确定。脚本在启动时运行很好。当我做GUI项目时,我的脚本位于
/home/pi/
目录中。这个脚本的python代码从我桌面上的一个文件夹(Raspbian Stretch)中打开了图片。也许你也可以尝试从桌面文件夹加载图片。如果这样做有效,您就有了一个参考,您可以尝试其他可能有效的位置选项。我真的希望,这对你有帮助。祝你好运:)