[英]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:
ssh
ing to it or connecting a monitor/keyboard. ssh
荷兰国际集团到它或连接显示器/键盘。 sudo nano /etc/inittab
to open the inittab for editing. sudo nano /etc/inittab
打开inittab进行编辑。 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
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 sudo nano /etc/rc.local
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. python /path/to/mycoolscript.py &
), but still above the exit 0
line. python /path/to/mycoolscript.py &
),但仍要在exit 0
行之上。 &
included here . &
。 This "forks" the process, allowing other commands to run even if your script hasn't exited yet. 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上的两个步骤:
raspi-config
) raspi-config
) ~/.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/autostart
( ref ))
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.