Bash Zenity Python

Bash Zenity Python


I have three python versions installed in my computer, each one has differents modules and configurations, I want to have a single file that let me choose the python binary file that I wish to use; so that’s why I decided to write a Bash/Zenity script with a Python binary list allowed to use (and a GNome desktop shortcut entry to access it from the Main menu). Basically I will use Zenity an utility to display graphical dialog boxes from shell scripts, it is a rewrite of the gdialog command from GNOME 1; Zenity includes a gdialog wrapper script so that it can be used with legacy scripts. And then I’ll create a python.desktop shortcut script to access to my program from the GNome Main Menu.

Bash Zenity Python 1


exec $(
zenity \
  --list --radiolist \
  --width='380' --height='220' \
  --title='Python interpreter chooser' \
  --text='Select a python interpreter version below each one have differents modules and configurations.' \
  --print-column=3 \
  --column='' --column='Python name' --column='Binary path' \
  TRUE 'Python 2.6' '/usr/bin/python2.6' \
  FALSE 'Python 2.7' '/usr/local/bin/python2.7' \
  FALSE 'Python 3.1' '/usr/bin/python3.1'

Zenity has a bunch of widgets what we can use, like a calendar, popup information, notifications, text-boxes, an many others, you can use the command man zenity to get the full list of available resources. In the script I specified that I want to use the widget List, but not any list, I specified that I want a Radio list, then I set the width and height of the window container of the widget, the title and a short description; in the next lines, was specified the column number where I have the value to use, in the Radio list widget there is a column by default, the radio column list, and this is by default the column which will be printed in the terminal when we choose an option, I don’t want to print that value nor the Python title (second column), but the third column value: the python binary path; to do this we can use the parameter --print-column=COLUMN_NUMBER and the parameter --column='COLUMN_TITLE' to specify the title of each column, in this case I specified that the first columns won’t be have a title, but the second and the third will be identified by the names Python name and Binary path respectively; using the boolean values I specified that the default value of the radio list will be the first option.

Desktop shortcut

XDG-Utils (part of the Portland project) is the package contains utilities for integrating applications with the desktop environment, regardless of which desktop environment is used; one of their are the Desktop entries:

  • xdg-desktop-menu - Install desktop menu items
  • xdg-desktop-icon - Install icons on the user’s desktop
  • xdg-icon-resource - Install icon resources
  • xdg-mime - Gather MIME information about a file
  • xdg-open - Open a URL in the user’s preferred application that handles the respective URL or file type
  • xdg-email - Open the user’s preferred email client, potentially with subject and other info filled in
  • xdg-screensaver - Enable, disable, or suspend the screensaver

All the shortcuts shown in the GNome Main Menu are placed in this location (in UNIX systems) /usr/share/applications and /home/USERNAME/.local/share/applications/ for user’s desktop entries, but this last shortcuts are only visible for the user owner of the folder; so that’s why I decided to place the python.desktop file in the root system path letting other users access this shortcurt from the global menu.

$ ls -lhas /usr/share/applications/python.desktop
4.0K -rw-r--r-- 1 root root 526 Jun 24 2010 /usr/share/applications/python.desktop
[Desktop Entry]
Name=Python interpreter chooser
Comment=Select a python interpreter version below each one have differents modules and configurations.

Pay attention to the first and second lines of the desktop entry, because those are the most important parameters taken for the XDG-Desktop-Menu and the Terminal=true parameter is used by the shortcut to execute the python interpreter chosen from the zenity radio list widget and continue running the program, if you change the value of this parameter to false the exec will execute the interpreter chosen but the window will be closed automatically (that’s not obviously good).

Do you have a project idea? Let's make it together!