问题描述
我想运行一个在启动时执行 GUI 的 python 脚本(当 pi 启动时).但是我在屏幕上没有看到任何 GUI,但是当我打开终端时,我的程序会自动执行并出现 GUI.此外,我的程序在执行时需要互联网连接,但 pi 稍后连接到 wifi,我的脚本首先执行并以未连接到互联网结束.
I want to run a python script which executes a GUI on startup(as pi boots up). But I don't see any GUI on screen but when I open terminal my program executes automatically and GUI appears. 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.
在 pi 正常启动并且 pi 连接到互联网后,我的 python 脚本有什么办法可以执行
Is there any way my python script executes after pi boots up properly and pi connected with internet
推荐答案
Raspian 上的两个步骤:
Two steps on Raspian:
- 确保您启动到 GUI(可以通过 sudo
raspi-config
进行设置) - 编辑
~/.config/lxsession/LXDE-pi/autostart
并将您的 python 脚本添加到路径:例如@python/home/pi/your_script.py
- Make sure you boot into GUI (can setup via sudo
raspi-config
) - Edit
~/.config/lxsession/LXDE-pi/autostart
and add your python script to the path: e.g.@python /home/pi/your_script.py
如果路径为
~/.config/lxsession/LXDE-pi/autostart
或
~/.config/lxsession/LXDE/autostart
我建议一次尝试一个.(旧版本可能使用此路径 /etc/xdg/lxsession/LXDE-pi/autostart
(ref))
I recommend trying one at a time.(Older version might use this path /etc/xdg/lxsession/LXDE-pi/autostart
(ref))
这应该会在 UI 初始化后运行脚本,但您不能保证 WiFi 已连接.我建议修改你的 python 脚本以检查它是否先连接,如果几秒钟后没有重试,然后按预期执行其余部分.
This should run the script after the UI initialises but you don't have any guarantee WiFi is connected though. 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.
这篇关于树莓派:启动时自动运行 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!