Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - raspberry pi : Auto run GUI on boot

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.

Is there any way my python script executes after pi boots up properly and pi connected with internet

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Without knowing you Pi setup it's a bit difficult. But with the assumption you're running raspbian with its default "desktop" mode:

  1. Open a terminal on your Pi, either by sshing to it or connecting a monitor/keyboard.
  2. First we need to allow you to login automatically, so sudo nano /etc/inittab to open the inittab for editing.
  3. Find the line 1:2345:respawn:/sbin/getty 115200 tty1 and change it to #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. Type Ctrl+O and then Ctrl+X to save and exit
  5. Next, we can edit the 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. This will launch X on startup, which allows other applications to use graphical interfaces.
  7. Add the command you'd like to run below the previous line (e.g python /path/to/mycoolscript.py &), but still above the exit 0 line.
    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.

Now when you power on your Pi, it'll automatically log in, start X, and then launch the python script you've written!

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...