Made using Canva

Creating unique Dialog Boxes using Zenity 💻

Rahul Sil
8 min readSep 26, 2021

--

Zenity is an open-source and cross-platform application that displays GTK+ Dialog Boxes in command-line and using shell scripts. It allows to ask and present information to/from the shell in Graphical Boxes. The application lets you create Graphical dialog boxes in the command line and makes the interaction between user and shell very easy.

With zenity, we can create GUI dialog boxes with simplicity and not going into too many complex things. It has few features but is simpler than more complex GUI creation tools.

Other scripting languages such as Perl and Python can be used to construct full-scale GUI applications, but the zenity program enables a shell script to interact with a GUI user…. [The] user interface is not as refined as one that could be provided by a full-featured GUI application, but it is perfectly suitable for simple interactions.

— Chris Tyler

Installation of Zenity 🚀

Zenity is mostly comes installed in Linux-based operating systems. You can check that using the below command —

zenity --version

It will tell the version of zenity that is installed on your system. As you can see I have the zenity version 3.28.1 installed on my system.

If in case it is not installed, then you can run the following command to install it -

sudo apt-get install zenity -y		[on Debian based systems]

yum install zenity -y [on RedHat based systems]

Moreover, you can also build it from the source files, download the latest Zenity source package (i.e. current version 3.8) using the following link.

http://ftp.gnome.org/pub/gnome/sources/zenity/

Now let’s create some dialog boxes using Zenity. 🙌🔥

Note: I have created an alias to adjust the width and height of the dialog boxes. Also when we display something with zenity a warning message comes, in order to remove that redirected that to /dev/null.

alias zenity='zenity --width=600 --height=600 2> /dev/null'

You can set this alias as per your needs by changing the width and the height of the dialog box.

  1. Let’s first display the calendar.🗓️📅
zenity --calendar
The output of the calendar option.

When we choose the date and click on “OK” then the chosen date is returned to the standard output.

Date output returned to the standard output- terminal.

We can change the default date format that is returned to the standard output.

zenity --calendar --date-format=%d/%m/%y
Date format changed.

Now there are some extra options that can be used with the calendar option of zenity.

zenity --calendar --text="<your message>" \
--day=15 --month=9 --year=2021 \
--title="<your title>"

2. Next we have the entry option in zenity with which we can enter some data.

It also has some options using which we can create something.

zenity --entry --text="Enter your name" --entry-text="<your text>"

We can also hide the text when we have to enter some sensitive data like a password.

zenity --entry --text="Enter the password"  --hide-text

Whatever we write in the input box is returned as an output to the standard terminal.

3. We can also use zenity to show some error pop-ups. ❌

zenity --error --text="Not Authorized"

4. We select files also using the zenity command. 🗃️

zenity --file-selection

It opens the file explorer from where we can select a file.

For selecting multiple files we use the following command -

zenity --file-selection --multiple

Now if you are using zenity in a shell script and want to overwrite a file, but before that ask yes/no if the file exists, then we use the following command —

zenity --file-selection --confirm-overwrite --save

If we do not use the “- -confirm-overwrite then without asking the file will be overwritten.

Then we have an option for selecting directory only.

zenity --file-selection --directory

These were some of the combinations of file selection of zenity. Other options you can definitely explore from the man page of the command.

man zenity

5. With the help of zenity we can also display an information pop-up window.

zenity --info --title="Information Box" --text="<your message>"

6. We can also create a checklist with the zenity dialog box.

zenity --list --title="<your title>" --checklist --editable \
--column="<column 1 name>" --column="<column 2 name>" \
--text= "<column 1 data>" "<column 2 data>" "<column 1 data>" "<column 2 data>"

There are other options also which can be used and can be used as per the use case.

7. For notification 🔔purposes also we can use the zenity command.

zenity --notification --window-icon=<path to picture>               --text="<text in the notification bar"

Note: This “- -window-icon” option, for now, works only with the notification option of zenity.

8. We can display a progress bar with zenity when we are downloading any software.

zenity --progress --text="<your text>" --percentage=<INT>

We can have a pulsating progress bar also.

zenity --progress --text="<your text>" --percentage=<INT> --pulsate

9. We can have question dialogs as well 🙋‍♂️

zenity --question --text="<your question>"

10. We can read the data of the file 📁 using the zenity dialog boxes.

zenity --text-info --filename=<filename>

The file can also be edited.

zenity --text-info --filename=<filename> --editable

When we use this option the contents of the file are sent to the standard output.

11. Warning ⚠️ dialog box using zenity

zenity --warning

12. We have a scale dialog box in zenity using which we can select a value from a range of values.

zenity --scale

When we normally use the command like this then we have the range from 0 to 100. We have options to change the minimum value, maximum value and also set an initial value.

zenity --scale --value=25
An initial value set in zenity scale.
Minimum and maximum values.

We have an option “- -print-partial ” using which we can print the values which we choose on the slider on the standard output.

The values are printed on the standard output.

13. When we have used paint 🎨 in our childhood we have used the color palette to choose the color that we want to fill in our drawing. Using zenity also we can create such a dialog box.

zenity --color-selection

As we can see the hex code of the color is shown in the dialog box. When we select the color then the RGB value is returned to the standard output.

RGB value returned to the terminal.

We can also have a color palette of specific solid colors.

zenity --color-selection --show-palette
Color palette.

Also if we know the hex code of color and want to look at what shade of color that is we can do that also.

zenity --color-selection --color=#490707
Color palette with the specific color hex code.

14. We can create a user sign-in dialog box.

zenity --password --username
Sign-in dialog box.

Caution: When you click “OK” the username and password are displayed as plain text on the standard output.

15. We can create a Google form kind of thing using zenity as well.

zenity --forms --text="Registration Form" --title="ARTH" \
--add-entry="Name" --add-entry="Email ID" \
--add-entry="WhatsApp Number"
Form created using zenity.

Here also, after we have entered the data, they are returned and printed on the standard output.

So these were some of the options that can be used with the zenity command and can help us a lot to make our shell scripting code a bit more user-friendly.🙌

I hope you liked this article. ️💖

I would definitely like to hear your reviews and feedbacks which will help me in improving my content in future technical blogs. 🗃️🙏

Follow me on medium as I will come up with articles on various technologies like Cloud Computing, DevOps, Automation, and their integration.

You can also check my LinkedIn profile and connect with me.

That’s all for now. Thank You !! 😊✌

--

--

Rahul Sil

I am a tech enthusiasts. I love exploring new technologies and creating stuff out of them !! ✌