Skip to content
Advertisement

Tkinter Python Auto Start on Raspberry Pi4

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.

JavaScript

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

JavaScript

Setup the system initrc to load the application

JavaScript

Comment out existing lines and add the following (change path to your script)

JavaScript

Create a systemd unit file to startup X window and run the application

JavaScript

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)

JavaScript

Reload the unit files and enable the new unit

JavaScript

Can check the status of the new service by the following command

JavaScript

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement