Modernize the Linux Terminal and Shell Scripts by adding GUI
--
Using command line is fun but it is always better to have an interactive GUI.
Many programming languages have a way of creating graphical interfaces. Some, like Python, provide special bindings for creating graphical user interfaces. Others, like Visual Basic, provide their own commands for creating GUIs.
Like tools for creating GUIs, Zenity allows quick creation of GUI. It has fewer features than more sophisticated/complex tools like whiptail and dialog.
What is Zenity?
Zenity enables us to create various types of simple dialog. These types of dialog can be defined in various ways. It is a free and a cross-platform program that allows the execution of GTK dialog boxes/UI toolkit in command-line and shell scripts. Besides Zenity, Native GUI notifications can also be used through the command line. These tools are usually pre-installed with linux distributions.
GTK-server is an open-source project released under the GNU General Public License. The GTK-server project aims to bring Graphical User Interface programming to any interpreted language using the GIMP Tool Kit (GTK) or XForms.
Zenity let’s us create GUI boxes for the following:
- Calendar
- File selection
- Forms
- List
- Notification icon
- Message
- Error
- Information
- Question
- Warning
- Password entry
- Progress
- Text entry
- Text information
- Scale
- Color selection
Now, what’ so special about Zenity?
Other scripting languages can also be used to create full-featured GUI applications, such as Perl and Python. However the zenity program enables a shell script to interact with a GUI user. The user interface might not as refined as one that could be provided by a full-featured GUI application, but it is perfectly suitable for simple interactions.
To learn about shell scripts: Shell Scripting. You might have came across the word… | by Gursimar Singh | Medium
Normally, Zenity detects the terminal window from which it was launched and keeps itself above that window. This behavior can be disabled by unsetting the WINDOWID environment variable.
Message boxes
Various types of message boxes can be created with Zenity
- Information message box
zenity --info
$ zenity --info --title="Installation complete" --text="Please restart the system" --no-wrap
- Warning message box
zenity --info
$ zenity --warning --title="Battery low" --text="Please plug your computer" --no-wrap
- Error message box
zenity --error
$ zenity --error --title="Error" --text="Unable to delete app.log (ERR: 565)" --no-wrap
- Error message box
zenity --question
if zenity --question --title="Confirm deletion" --text="Are you sure you want to delete this file?" --no-wrap
then
zenity --info --title="Success" --text="app.log was removed" --no-wrap
fi
- Progress box
When the script is processing data, a progress bar can be displayed ( — progress). Zenity offers two kinds. One is the pulsating bar where a box moves back and forth (use the — pulsate parameter) and stops after an end-of-file (EOF) symbol is reached. The second progress bar fills according to percentage. To make such a window, make a command like this -
$ zenity — progress — text=”Working hard or hardly working?” — percentage=0 –auto-close
Notifications
$ zenity --notification --window-icon="info" --text="There are system updates necessary!"
Zenity has a feature to send notifications but notify-send gives us a lot more freedom for adjustments.
Input element
- Calendar input box
zenity --calendar
I=$(zenity --calendar)
zenity --info --text="Selected date: $I" --no-wrap
- String input box
zenity --calendar
NAME=$(zenity --entry --title="Please enter your name")
if [ -n "$NAME" ]
then
zenity --info --text="Hello $NAME" --no-wrap
fi
- Similarly,
zenity --password
can be used to capture a secret string from the user such as a password or a PIN number. Moreover, password input allows us to enable the username field as well. Then it will be returning username and the password separated with | character.
To enter a username and password via Zenity, use a command like this -
PRIVATE='zenity --password --username
The username and password are saved to the variable PRIVATE. The username parameter is optional, so programmers can use this when only a password is needed. Again, be aware of security issues. If the user closes the window without entering data and clicking “Okay”, then $? equals “1” like all other Zenity windows.
- File selection dialog
zenity --file-selection
To select files, we use the “ — file-selection” dialog. “ — multiple” allows more than one file to be selected. “ — directory” permits the selection of directories. When saving the list of selected files to a variable, the “ — separator=STRING” parameter allows the programmer to set the string or character to be used as a division for items in a list. Be careful that the selected files do not contain the string that is to be used as a separator. Otherwise, the list will appear to have different/more files. “ — filename=PATH” sets which file or directory is highlighted by default. To use this same dialog to save a file, use the “ — save” parameter.
$ zenity --file-selection --title="Select a file"(or)FILE=`zenity — file-selection — multiple — directory — separator=”+++”`
To select a color, use the — color-selection dialog. “ — color=#” sets the initially selected color, and “ — show-palette” displays the palette set. The code may look like this — COLOR=`zenity — color-selection — show-palette`.
List selections
$ zenity --list \
--title="Choose your OS" \
--column="OS" --column="Interface" \
Ubuntu Unity \
"OS X" Marble \
FreeBSD "Command line" \
Fedora GNOME \
Minix Command_line \
Pidora XFCE \
Lubuntu LXDE \
"MS-Windows" Metro
Access Keys
An access key is a key that enables you to perform an action from the keyboard rather than use the mouse to choose a command from a menu or dialog. Each access key is identified by an underlined letter on a menu or dialog option.
Some Zenity dialogs support the use of access keys. To specify the character to use as the access key, place an underscore before that character in the text of the dialog. The following example shows how to specify the letter ‘C’ as the access key:
"_Choose a name".
Exit Codes
Zenity returns the following exit codes:
General Options
All Zenity dialogs support the following general options:
--title=title
Specifies the title of a dialog.
--window-icon=icon_path
Specifies the icon that is displayed in the window frame of the dialog. There are 4 stock icons also available by providing the following keywords — ‘info’, ‘warning’, ‘question’ and ‘error’.
--width=width
Specifies the width of the dialog.
--height=height
Specifies the height of the dialog.
--timeout=timeout
Specifies the timeout in seconds after which the dialog is closed.
Help Options
Zenity provides various help options. The general ones are:
--help
Displays shortened help text.
--help-all
Displays full help text for all dialogs.
--help-general
Cross-platform compatibility
As of 2012, Zenity is available for Linux, BSD and Windows. A Zenity port to Mac OS X is available in MacPorts and Homebrew.
As of 2018, Zenity ports for Windows are available: zenity-windows (based on version 3.20.0) and winzenity (based on 3.8.0 / statically linked)
There is no built-in scripting capabilities available with Zenity so it is dependent on an interpreter for processing. To create a script that runs on more than one platform without extensive modifications, it would be best to use an interpreter that is available on the widest range of operating systems. One option is Python in combination with the PyZenity library.
python-zenity
python-zenity is a library for python wich inspired by Zenity. When you write scripts, you can use python-zenity to create simple dialogs that interact graphically with the user.
Requirements
- Python 2.x (x>6)
- PyGTK
Installation
Install using pip :
$ pip install python-zenity
Or clone the repo :
$ git clone https://github.com/poulp/python-zenity.git
$ cd ./python-zenity
$ python setup.py install
For example, a simple dialog :
from pythonzenity import Calendar
result = Calendar(title="Awesome Calendar",text_info="Your birthday ?")
print result
BONUS:
The bash code to show the CPU idle time in a info dialog is:
$ zenity --info --text=$(top -n 1 | grep %Cpu | awk '{print $8}') --title="CPU Idle Time"
We can create it with just one simple line of code.