简体   繁体   English

raspberry pi:引导时自动运行GUI

[英]raspberry pi : Auto run GUI on boot

I want to run a python script which executes a GUI on startup(as pi boots up). 我想运行一个python脚本,该脚本在启动时执行GUI(随着pi启动)。 But I don't see any GUI on screen but when I open terminal my program executes automatically and GUI appears. 但是我在屏幕上看不到任何GUI,但是当我打开终端时,我的程序会自动执行并显示GUI。 Also, my program requires an internet connection on execution but pi connects to wifi later and my script executes first and ends with not connecting to the internet. 另外,我的程序在执行时需要Internet连接,但是pi之后会连接到wifi,并且我的脚本会先执行,最后以不连接到Internet结束。

Is there any way my python script executes after pi boots up properly and pi connected with internet pi正确启动并pi与互联网连接后,我的python脚本有什么执行方法吗

Without knowing you Pi setup it's a bit difficult. 不了解您的Pi设置会有些困难。 But with the assumption you're running raspbian with its default "desktop" mode: 但是假设您正在使用默认的“桌面”模式运行raspbian:

  1. Open a terminal on your Pi, either by ssh ing to it or connecting a monitor/keyboard. 在您裨打开终端,或者通过ssh荷兰国际集团到它或连接显示器/键盘。
  2. First we need to allow you to login automatically, so sudo nano /etc/inittab to open the inittab for editing. 首先,我们需要允许您自动登录,因此sudo nano /etc/inittab打开inittab进行编辑。
  3. Find the line 1:2345:respawn:/sbin/getty 115200 tty1 and change it to #1:2345:respawn:/sbin/getty 115200 tty1 找到行1:2345:respawn:/sbin/getty 115200 tty1并将其更改为#1:2345:respawn:/sbin/getty 115200 tty1
  4. Under that line, add 1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1 . 在该行下面,添加1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1 Type Ctrl+O and then Ctrl+X to save and exit 键入Ctrl + O,然后按Ctrl + X保存并退出
  5. Next, we can edit the rc.local. 接下来,我们可以编辑rc.local。 sudo nano /etc/rc.local
  6. Add a line su -l pi -c startx (replacing pi with the username you want to launch as) above the exit 0 line. exit 0行上方添加su -l pi -c startx (用您要启动的用户名替换pi )。 This will launch X on startup, which allows other applications to use graphical interfaces. 这将在启动时启动X,从而允许其他应用程序使用图形界面。
  7. Add the command you'd like to run below the previous line (eg python /path/to/mycoolscript.py & ), but still above the exit 0 line. 将您想运行的命令添加到上一行下面(例如python /path/to/mycoolscript.py & ),但仍要在exit 0行之上。
    Note the & included here . 注意此处的& This "forks" the process, allowing other commands to run even if your script hasn't exited yet. 这“分叉”了该过程,即使您的脚本尚未退出也允许其他命令运行。 Ctrl+O and Ctrl+X again to save and exit. 再次按Ctrl + O和Ctrl + X保存并退出。

Now when you power on your Pi, it'll automatically log in, start X, and then launch the python script you've written! 现在,当您打开Pi的电源时,它将自动登录,启动X,然后启动您编写的python脚本!

Also, my program requires an internet connection on execution but pi connects to wifi later and my script executes first and ends with not connecting to the internet. 另外,我的程序在执行时需要Internet连接,但是pi之后会连接到wifi,并且我的脚本会先执行,最后以不连接到Internet结束。

This should be solved in the script itself. 这应该在脚本本身中解决。 Create a simple while loop that checks for internet access, waits, and repeats until the wifi connects. 创建一个简单的while循环,检查互联网访问,等待并重复直到wifi连接为止。

Two steps on Raspian: 在Raspian上的两个步骤:

  1. Make sure you boot into GUI (can setup via sudo raspi-config ) 确保您启动到GUI(可以通过sudo raspi-config
  2. Edit ~/.config/lxsession/LXDE-pi/autostart and add your python script to the path: eg @python /home/pi/your_script.py 编辑~/.config/lxsession/LXDE-pi/autostart并将您的python脚本添加到路径:例如@python /home/pi/your_script.py

It depends on the version of raspian if the path is 如果路径是

~/.config/lxsession/LXDE-pi/autostart

or 要么

~/.config/lxsession/LXDE/autostart

I recommend trying one at a time. 我建议一次尝试一次。 (Older version might use this path /etc/xdg/lxsession/LXDE-pi/autostart ( ref )) (较旧的版本可能使用此路径/etc/xdg/lxsession/LXDE-pi/autostartref ))

This should run the script after the UI initialises but you don't have any guarantee WiFi is connected though. 这应该在UI初始化后运行脚本,但是您不能保证WiFi已连接。 I recommend ammending your python script to check if it's connected first and if not retry after a few seconds until it is, then execute the rest as expected. 我建议修改您的python脚本,以检查它是否先连接,如果几秒钟后不重试,直到连接成功,然后按预期执行其余操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM