Search code examples
pythonlinuxtkinterraspberry-pi3boot

Python/Tkinter App run on Raspberry Pi boot


I have created a python app with a GUI made with Tkinter on a Raspberry Pi 3. I am currently trying to set up the Pi so that it runs my program on boot. I don't need anything like the desktop or such to load unless my program crashes.

I have tried looking for ways to accomplish this such as this post from the Raspberry Pi Stack Exchange but haven't been able to get anything to work. In fact when I tried out the suggestions in that link I hit a snag that made me have to reinstall Raspbian. I am wondering if anyone over here knows how to get this working.


Solution

  • You may not need the desktop, but tkinter does. So you can't disable the graphical boot.

    First, make your python file executable with this command:

    chmod +x my_program.py
    

    Then add a shebang to the top of the file. For python2, the first line of the file must be:

    #!/usr/bin/env python
    

    Now you can launch your python program like any other program, including adding it to autostart. When I did this I just run the command

    nano /home/pi/.config/lxsession/LXDE-pi/autostart
    

    to start the nano text editor and add the line

    @/home/pi/folder/my_program.py
    

    to the end.