I am building my first tkinter application which works perfectly until I try to auto start it at boot. I have researched this topic and tried numerous things but nothing works correctly.
I am running RPi4 with raspbian buster, python 3.7.3 and want this application to run on the RPI touch screen without requiring a user to login.
I found this link which looks like I should be able to start my app from the terminal without loading the desktop by invoking a window manager like X openbox or something similar. I did find that if I create a ~/.xinitrc file and add the following line, I can start the window from the command prompt by typing startx.
exec sudo /usr/bin/python3 /home/$user_name/robot/robot.py
I have tried starting the program using systemd but to no avail. Would certainly appreciate some help on this.
Advertisement
Answer
Ok so after a week of beating my head on the desk I finally found a solution that works perfect. This loads the GUI application before the command prompt login with the raspberry pi GUI disabled and when the application is closed for any reason it returns to a logged out command prompt.
I want to clarify that I have disabled the default pi user and have created another user account for this project. I have located my entire python/tkinter project in the ‘/home/$user_name/robot’ folder and have given this new user the same permissions as the pi user.
Enable boot to command line only
sudo raspi-config Select => Boot Options / Desktop-CLI / Console
Setup the system initrc to load the application
sudo nano /etc/X11/xinit/xinitrc
Comment out existing lines and add the following (change path to your script)
/usr/bin/python3 /home/$user_name/robot/robot.py
Create a systemd unit file to startup X window and run the application
#Change the name.service to whatever you want it to be sudo nano /lib/systemd/system/robot.service
In the new file add the following lines (:0.0 is RPi Display Port and Xauthority points to the user profile you wish to run the app under)
[Unit] Description=Start Robot After=graphical.target [Service] Environment=DISPLAY=:0.0 Environment=XAUTHORITY=/home/$user_name/.Xauthority ExecStart=startx KillMode=process TimeoutSec=infinity [Install] WantedBy=graphical.target
Reload the unit files and enable the new unit
sudo systemctl daemon-reload sudo systemctl enable robot.service
Can check the status of the new service by the following command
sudo systemctl status robot.service
Now reboot the system and the python application with tkinter gui should be displayed before the command prompt is shown. If the app is exited for any reason, the command prompt login should be shown.
Thanks to @acw1668 for sharing THIS LINK which helped a ton. THIS is a great reference for systemd and unit file details.