Zenity - GUI for Shell Scripts

D

DevynCJohnson

Guest
Many programming languages have some way of coding a graphical interface. Some languages like Python have special bindings (PyGTK, PYQT, etc.) and others, like Visual Basic, have their own commands for designing GUIs. As a result, many shell-script writers probably wish there were something like that for shell scripts. Well, there is a special way to code a GUI for scripts in the script itself. A special program called "Zenity" can be used in scripts to make GTK+-based windows. Zenity is used like other commands in a script with parameters and such.

NOTE: Readers should have an understanding of shell scripting to fully grasp this article. Although, knowing how to script is not entirely necessary.

Obviously, the script writer would make a script as usual. Now, the programmer can add Zenity commands or replace echo and read commands with windows that perform the same function.

The basic usage of Zenity is like this - zenity --info --text="blah blah blah"

zenity_01.png


"zenity" is the command. "--info" declares the type of box/window. "--text=" creates the desired text to be displayed by this info window. The text parameter can display the output of commands and the value of variables. As many scriptwriters will see, Zenity uses the same syntax as shell scripts. Displaying the output of a command is as easy as this - zenity --info --text=`whoami`

NOTE: Zenity also works in batch files (in MS-Windows). Here, Zenity would use the same syntax/rules of batch files. Also, keep in mind that Zenity is a third-party program and not a language, although I seem to make it sound like it is some programming language.

Zenity supports a few different kinds of dialogs as seen below. One useful parameter that can be used on all of them is the "--title=TEXT" parameter, where "TEXT" is the name desired for the window.

zenity_05.png


--calendar
--color-selection
--file-selection
--forms
--list
--notification
--password
--progress
--scale
--entry
--text-info
--error
--warning
--question
--info

The message dialogs (--info, --question, --warning, --error) display some message via the "--text=" parameter. These dialogs create a window that displays a message. The differences between them lies in the icon shown and the type of buttons. For instance, "--question" makes a window with two buttons (yes and no) and a question-mark icon, and "--warning" has one button (okay) and an exclamation-point icon. These message dialogs can be used in place of the echo command.

zenity_02.png

zenity_03.png


The "--text-info" dialog is used to display a text file. For example, "zenity --text-info --filename="/home/user/file.txt" would open a text file to be viewed. "--font=FONT" is a parameter used to specify the font to be used. "--editable" makes the displayed text editable, and the text will be sent to stdout when the dialog is closed. Programmers can use "--text-info" to display license terms and have a check-box for accepting the terms (--checkbox=TEXT). HTML is support via the "--html" parameter. The html parameter is needed to use the url parameter which displays webpages in the window (--url=LINK).

Some of you may wonder how the script can execute differently based on whether the check-box associated with "--text-info" was checked or unchecked. The "$?" shell variable equals "1" when the box is unchecked and "0" when checked. Then, the programmer can have a decision construct that executes code based on the user's choice.

zenity_04.png


NOTE: The $? variable prints the error code of the previously executed command. No actual error is occurring though with Zenity unless you get a number other than zero or one.

Users can enter text in Zenity via the "--entry" dialog. The code {zenity --entry --entry-text="Type" --text="Bang on the keyboard"} makes a window with text giving the user some information (like directions or a question) and an input box. The typed text can be retrieved through the $? variable which will also include "/n0" if no errors occurred. The "sed" or "awk" command can be used to remove the zero and newline when only the entered text is needed. The "--hide-text" parameter makes the text entered in the input box look like dots. This is used when entering private data like a password.

zenity_07.png


NOTE: No encryption is used in Zenity. Please be aware of security risks.

The "--scale" dialog makes a window with a slider. To use a number slider, make code like this -

VALUE=`zenity --scale --text="Select a number" --value="20" --min-value="0" --max-value="200" –step="5"`

zenity_08.png


The selected number will be saved in the variable "VALUE". The backticks are a special form of syntax used in shell scripting to indicate that the code inside is to be executed and that it is not a string. The "--text=TEXT" displays the desired text in the window. The "--value=#" parameter specifies where to start the slider (default = 0). The "--min-value=#" and "--max-value=#" set the limits of the slider (default = 0 and 100, respectively). The "--step=#" parameter sets the incremental value (default = 1). Using the arrow keys to move the slider will yield the set stepping, but not so when using the mouse (a bug perhaps?). To hide the number shown above the slider, use "--hide-value".

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

zenity_10.png


The "--auto-close" parameter makes the window close when 100% is reached. The "--percentage=%" indicates where the bar starts. The bar is filled by piping an integer (number) to Zenity. This is done as seen below. After a command executes, the next echo command tells Zenity to fill the bar more. Once the last command executes, then the bar fills to zero and then closes (if --auto-close is present).

#Progress Bar
(echo "40"; COMPLEX_COMMAND; echo "60"; COMMAND) | zenity --progress --text="Working hard or hardly working?" --percentage=0 --auto-close

#Pulsating Bar
(COMMAND; COMMAND; COMMAND; COMMAND) | zenity --progress --text="Working hard or hardly working?" --percentage=0 --auto-close

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.

zenity_11.png


"--notification" displays a notification bubble or a window depending on the desktop interface. (zenity --notification --text=TEXT)

To select file(s), 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. Below is an example of the code.

FILE=`zenity --file-selection --multiple --directory --separator="+++"`

zenity_13.png


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`.

zenity_14.png


zenity_15.png


A calendar dialog (--calendar) also exists. "zenity --calendar" will show a calendar with the current date highlighted. The "--day=#", "--month=#", and "--year=#" parameters set the time to be highlighted and shown when the window appears. The "--date-format=strftime" allows the programmer to set the time format in strftime format (where month is %m, year is %y, etc.). "--text=TEXT" is used to display a message on the window. The selected date can be saved to a variable.

zenity_16.png


"--list" makes a list dialog. The "--column=NAME" parameter can be used as much as needed to make a column of data. To make such a dialog, use a format like the one seen below. If a field contains spaces or other non-alphanum characters, place that field in quotes to indicate that the string is one set of data. If radio-buttons or check-boxes are included, make sure a column is made for them. The "--checklist" and "--radiolist" parameters place check-boxes and radio-buttons in the first column, respectively. "--editable" allows the data to be changed.

Code:
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

zenity_17.png


The last dialog offered by Zenity is forms (--forms) which is used to enter many sets of data. For private data, the "--add-password=FIELD_NAME" parameter can be used to make an input box that does not display the contents. For text that can be seen, use the "--add-entry=FIELD_NAME" parameter. "--text=TEXT" displays a message on the window. "--add-calendar=FIELD_NAME" makes a calendar which is used to select a date. The calendar's output format is specified with "--forms-date-format="strftime". The "--separator=STRING" parameter is used to set how the fields are separated from each other in the output. After the Zenity command and its parameters, ">>" can be used to direct the data to a file. (zenity --forms --PARAMETERS >> data.csv)

Now, that you know about Zenity, you can enhance your favorite scripts and make new ones that are more advanced than what you made before. Enjoy!

Debate

Is Zenity a suitable GUI for shell-scripts?
 

Attachments

  • slide.JPG
    slide.JPG
    40.1 KB · Views: 120,513


great job¡¡¡, i have a cuestion, i need show coplete output of cmd...powersave -b, but zenity --text=`powersave -b` only show the firts output...battery 1: and no more.
do you know how i can do that???
thank for your time¡¡¡
 
great job¡¡¡, i have a cuestion, i need show coplete output of cmd...powersave -b, but zenity --text=`powersave -b` only show the firts output...battery 1: and no more.
do you know how i can do that???
thank for your time¡¡¡

Instead of
Code:
zenity --text=`powersave -b`
try
Code:
zenity --info --text="`powersave -b`"

This helps Zenity to receive the "newline" characters from the shell.

I can also prove and test this using the two commands below.

Works
Code:
zenity --info --text="`ls`"

Only one line appears
Code:
zenity --info --text=`ls`
 
Instead of
Code:
zenity --text=`powersave -b`
try
Code:
zenity --info --text="`powersave -b`"

This helps Zenity to receive the "newline" characters from the shell.

I can also prove and test this using the two commands below.

Works
Code:
zenity --info --text="`ls`"

Only one line appears
Code:
zenity --info --text=`ls`

ok that's work fine, thanks¡¡¡
 
Everybody needs a timer: see user daht cavenet daht com and check out timer.txt, also ftp-user.txt
Both use zenity, & wmctrl is used to set window geometry. Also bash subshells!
 
Hi Devyn,

I would like to ask some questions.
1. If I would like to make some dialog boxes using Zenity that appear depending upon the timers I set, for example, the first dialog box appears after 1 minute, then the next appears after 8 minutes, etc. How can I do it?
2. If I would like to make an interactive dialog boxes between PCs, for example, PC 1 shows the question "what do you like?" and the answer buttons show "mango" and "apple" instead of "Yes" and "No", then the answer (either mango or apple) will be shown and saved on the PC2 screen. I also want to save the timing of the answer clicking on PC1 screen, when the user of PC1 answers either mango or apple. How can I do that using Zenity?
Many thanks
 
@Fitri Trapsilawati
You need to understand shell-scripting very well to create such a script. It may be better to write such a program in Python.
As for the timer, look into using one of the commands listed below. Other commands and methods may be available.
Code:
watch -n $seconds COMMANDS
sleep $seconds
As for the buttons, why not use the list dialog (--list)? That for many of the dialogs, I show examples like
Code:
PRIVATE=`zenity --password --username`
You can then send the data (in the variable) to a file. As for sending it to another computer, that involves networking. Perhaps, you should use HTML5+JavaScript+PHP+SQL for such an application.
 
Soooo..... I'm using Zenity in HergOS (a custom linux i'm working on), and i have an icon theme set up. How can i use that?
 
Have been using Kdialog and much more impressed by this program, especially with --form option.

Want to do a timer for cooking and/or to display a reminder. So really I am asking for a timer input which would do a countdown.
Interactive would be fantastic, just selecting a time range somehow would also work. Guessing others may want a count to up, how many days/years to retirement... When cooking when I click OK I would perform next task needed. Obviously scripted, but a countdown timer would be great!

Did note that the SLIDER only displays check-marks for the intervals and does not lock choices to an interval which to me should also be an option. Better than kdialog as I can at least see the value selected.

Did pipe of sleep and find to zenity and the progress bar really doesn't represent progress but does let you know it's running and when it's done. What processes would actually display a progress?


 
@mikwei welcome to linux.org :)

This Thread is 8 years old.

You would do better to start a new Thread in one of the Forums.

Cheers

Chris Turner
wizardfromoz
 

Members online


Top