Zenity – Creates Graphical (GTK+) Dialog Boxes in Command-line and Shell Scripts

[‘

n

GNU Linux, the operating system built on very powerful Kernel called Linux. Linux is famous for its command Line operations. With the invent of Linux in day-to-day and Desktop computing, nix remains no more biased towards command-Line, it is equally Graphical and developing Graphical application remains no more a difficult task.

n

Install Zenity in Linux
Zenity Display Graphical Boxes

n

Here in this article we will be discussing creation and execution of simple Graphical Dialog box using GTK+ application called “Zenity“.

n

What is Zenity?

n

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

n

There are other alternatives, but nothing compares to the simplicity of Zenity, specially when you don’t need complex programming. Zenity, a tool you must have your hands on.

n

Zenity Features

n

    n

  1. FOSS Software
  2. n

  3. Cross Platform Application
  4. n

  5. Allow GTK+ Dialog Box Execution
  6. n

  7. Command Line Tool
  8. n

  9. Support in Shell Scripting
  10. n

n

Usefulness

n

    n

  1. Easy GUI Creation
  2. n

  3. Less features than other complex Tools
  4. n

  5. Enables shell scripts to interact with a GUI users
  6. n

  7. Simple dialog creation is possible for graphical user interaction
  8. n

n

Since Zenity is available for all known major platforms, and based on GTK+ library, Zenity program can be ported to/from another platform.

n

Installation of Zenity in Linux

n

Zentity is by default installed or available in repository of most of the Standard Linux distribution of today. You can check if is installed onto your machine or not by executing following commands.

n

[emailxa0protected]:~$ zenity --version rnrn3.8.0

n

[emailxa0protected]:~$ whereis zenity rnrnzenity: /usr/bin/zenity /usr/bin/X11/zenity /usr/share/zenity /usr/share/man/man1/zenity.1.gz

n

If it’s not installed, you can install it using Apt or Yum command as shown below.

n

[emailxa0protected]:~$ sudo apt-get install zenitytt[on Debian based systems]rnrn[emailxa0protected]:~# yum install zenitytttt[on RedHat based systems]

n

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

n

    n

  1. http://ftp.gnome.org/pub/gnome/sources/zenity/
  2. n

n

Zenity Basic Dialog Boxes

n

Some of the basic Dialogs of Zenity, which can be invoked directly from the command-line.

n

1. How about a quick calendar dialog?

n

[emailxa0protected]:~# zenity --calendar

n

Calendar Dialogue
Calendar Dialog

n

2. An error Dialog Box

n

[emailxa0protected]:~# zenity --error

n

Error Dialogue
Error Dialog

n

3. A General text Entry Dialog Box

n

[emailxa0protected]:~# zenity --entry

n

Entry Dialogue
Entry Dialog

n

4. An Information Dialog

n

[emailxa0protected]:~# zenity --info

n

Info Dialogue
Info Dialog

n

5. A question Dialog box

n

[emailxa0protected]:~# zenity --question

n

Question Box
Question Box

n

6. A progress Bar

n

[emailxa0protected]:~# zenity --progress

n

Progress Bar
Progress Bar

n

7. Scale Dialog

n

[emailxa0protected]:~# zenity --scale

n

Scale Box
Scale Box

n

8. A Password Dialog

n

[emailxa0protected]:~# zenity --password

n

Password Box
Password Box

n

9. A Form Dialog box

n

[emailxa0protected]:~# zenity --forms

n

Forms
Forms

n

10. An about Dialog

n

[emailxa0protected]:~# zenity --about

n

About Zenity
About Zenity

n

Create Shell Script Dialog

n

Now we would be discussing Zenity Dialog creation using simple shell scripts here. Although we can create single Dialog by executing Zenity commands directly from the shell (as we did above) but then we can’t link two Dialog boxes in order to obtain some meaningful result.

n

How about an interactive dialog box which takes input from you, and shows the result.

n

#!/bin/bash rnfirst=$(zenity --title="Your's First Name" --text "What is your first name?" --entry) rnzenity --info --title="Welcome" --text="Mr./Ms. $first" rnlast=$(zenity --title="Your's Last Name" --text "$first what is your last name?" --entry) rnzenity --info --title="Nice Meeting You" --text="Mr./Ms. $first $last"

n

Save it to ‘anything.sh‘ (conventionally) and do not forget to make it executable. Set 755 permission on anything.sh file and run the script.

n

[emailxa0protected]:~# chmod 755 anything.sh rn[emailxa0protected]:~# sh anything.sh

n

Enter Your First Name
Enter Your First Name

n

Welcome Dialogue
Welcome Dialog

n

Enter Your Last Name
Enter Your Last Name

n

Welcome Message
Welcome Message

n

About Script Description

n

The conventional shebang aka hashbang

n

#!/bin/bash

n

In the below line ‘first’ is a variable and the value of variable is Generated at run time.

n

    n

      n

    1. –entry‘ means zenity is asked to generate an text Entry box.
    2. n

    3. – title=‘ defines the title of generated text box.
    4. n

    5. —text=‘ defines the text that is available on text Entry box.
    6. n

    n

n

first=$(zenity --title="Your's First Name" --text "What is your first name?" --entry)

n

This line of the below script file is for generation of Information (–info) Dialog box, with title “Welcome” and Text “Mr./Ms.first”

n

zenity --info --title="Welcome" --text="Mr./Ms. $first"

n

This Line of the script is Similar to line number two of the script except here a new variable ‘last’ is defined.

n

last=$(zenity --title="Your's Last Name" --text "$first what is your last name?" --entry)

n

This last line of the script is again similar to the third line of the script and it generates information Dialog box which contains both the variables ‘$first’ and ‘$last’.

n

zenity --info --title="Nice Meeting You" --text="Mr./Ms. $first $last"

n

For more information on how to create custom dialog boxes using shell script, visit at following reference page Zenity.

n

    n

  1. https://help.gnome.org/users/zenity/stable/
  2. n

n

In the next article we would be integrating Zenity with more shell script for GUI user interaction. Till then stay tuned and connected to Tecmint. Don’t forget to give your valuable feedback in comment section.

n

‘]