Monday, August 26, 2019

How to Open a Python App via Desktop Icon on Raspberry Pi 3

Open a python application via desktop icon on raspberry pi 3
OS: Linux Raspian
Language: Python 2.7

There are two aspects to opening a python app via a desktop icon on raspberry pi 3, the first aspect is to make the python file executable and second is to create the.desktop file that contains the link to the executable python file location.

Executable Python File

Let’s discuss the first aspect. In order to make the python file executable follow the steps given below:-
  1. At the top of the python file include #!/usr/bin/env python this should be the first line of code. The version of python that is being used should also be mentioned as given in the image below
  2. Give admin permission to run the python file using the below code, the code must be executed on the raspberry pi terminal  window
Sudo chmod +x/file_location/name_of_file.py
3. Once the the two steps are completed reboot the system to save the changes


Create .desktop file

Second aspect of creating a .desktop file is as follows:-

  1. Navigate to the “Desktop” folder in the raspberry pi usually, this is the subfolder of the”pi” folder under the “home” folder.
  2. Create a new file
  3. In this, the newly created file include the below
  • Name – This is the name of the Desktop icon, this name will be displayed under the icon image on the desktop.
  • Comment – This is a non-mandatory tag that gives the description of the application
  • Exec –  This tag is the main execution script, write it in the way you would call the script from the python terminal. “Python” pretext is important here because without this execution of the script will fail. The location of the file can be anywhere that is accessible within the raspberry pi, not on a pen drive or external memory storage.
  • Icon – This tag gives the details for the icon image, this can be any image format png, jpeg etc. The location of the image file should be correct here as well.
  • Terminal – This tag if true will open the terminal window when the application is open else if false the terminal window will not. This is a good option if you want to debug the code.
  • Type – This is the type of script.
4. Save the file as a .desktop file.
5. Reboot the raspberry pi again.
6. Double click on the desktop icon to open the python application

No comments:

Post a Comment