Subscribe to RSS Subscribe to Comments Blog of Roy Chan

Blog of Roy Chan

Progress dialog box of Zenity

Zenity is the tool to display different types of GTK+ dialog box. It is very useful if you want to write a simple GUI shell script.

Apart from those simple yes-no/ok-cancel dialog box, zenity also provide interesting dialog box like the progress dialog box. The dialog box can be show with the commend “zenity --progress“. To make it meaningful, you may need to add more options of it.

The option “--text=string” control a short description show above the progress bar in the dialog box and “--percentage=num” set the percentage show in the progress bar.

You must not miss the following 4 options which applicable to all zenity dialog box. “--title=string” set the title of the dialog box and “--window-icon=path” set the window icon show beside the title. “--width=num” and “--height=num” control the size in pixels of the window. For example:

zenity --progress --title='Waiting...' --window-icon='/usr/share/pixmaps/gnome-about-logo.png' --text='Waiting for you to press Cancel' --percentage=50

Zenity progress dialog box

Sure, you won’t happy with the above functions. Let make the dialog box moving. Option “--pulsate” can make the progress bar beating right and left:

yes | zenity --progress --pulsate

Zenity pulsating progress box

Not satisfied? Me too! So, when will the Ok button press-able. Can the progress bar grow to show the progress of a task? Yes, it can. During the zenity show the progress dialog box, it also listen to the STDIN. If you type an number and press ENTER in the terminal you run zenity, you will see the number become the percentage show progress bar of the dialog box. If you start and line with “#”, type few words after it and press ENTER, the words will replace the description above the progress bar. In short, zenity --progress received the following format from STDIN:

# description1
# description2
# ...

Where num percentage of the progress bar and description change the progress description. The following script demo and explain more clear about the progress dialog box:

for x in 10 20 30 40 50 60 70 80 90 100
    echo $x
    echo "# Count to $x"
    sleep 1
done | zenity --progress --title='Counter'

Zenity progress box

You see that when the progress bar become 100%, the Ok button is activated.

Besides, you will found that if you press the “Cancel” button during the script working. The dialog box will disappear but you won’t see the prompt while script still running and not affected. You should expect the “Cancel” button will stop the script but it’s not. Option “--auto-kill” should be added to make it happen. It make the “Cancel” button kill parent process of zenity - the shell. Beware that if you type zenity on the command line, it will kill the shell and close the whole terminal. If you run zenity in a shell script, the script will be killed.

The last option you shall not missed is “--auto-close” which close the dialog automatically when the progress bar reach 100.

Many people demonstrate zenity progress with wget which show a progress dialog to show the progress while wget downloading file. But it seems the output format of wget was changed and all script I found online didn’t work. Here is my fix of the demo.

wget "$url" 2>&1 | \
     sed -u 's/^.* \+\([0-9]\+%\) \+\([0-9.]\+[GMKB]\) \+\([0-9hms.]\+\).*$/\1\n# Downloading... \2 (\3)/' | \
     zenity --progress --title='Download' --auto-kill --auto-close

It refine the wget output:

11550K .......... .......... .......... .......... .......... 21%  304K 2m51s
11600K .......... .......... .......... .......... .......... 21%  296K 2m50s
11650K .......... .......... .......... .......... .......... 22%  321K 2m50s
11700K .......... .......... .......... .......... .......... 22%  475K 2m49s

to zenity --progress format.

Share It: [] [Technorati] [Google Bookmark] [Yahoo MyWeb] [Furl]


  1. MarkitoXs
    December 8th, 2008 | 7:28

    Thank you a lot. Just found this after a google search (wget zenity progress) and actually used your workaround for a small script i am writing.


Leave a reply

  • :-)
  • :-D
  • :lol:
  • ;-)
  • :-P
  • :-(
  • :'(
  • :'-(
  • >:-(
  • :-O
  • :annoy:
  • :appeal:
  • :asleep:
  • Zzz...
  • :-Q
  • 8-)
  • B-)
  • :clap:
  • :enjoy:
  • :blush:
  • :shy:
  • :*
  • :inlove:
  • :inlove2:
  • :inlove3:
  • :love:
  • :allure:
  • 8D~~~
  • :amative:
  • :chatter:
  • :bored:
  • bored2:
  • X-O~~~
  • :-'|
  • 8-S
  • #_#
  • :dontknow:
  • :embarrased:
  • :excite:
  • :faint:
  • :fuzzy:
  • :plan:
  • :conceal:
  • :regret:
  • :punched:
  • :silent:
  • :-X
  • :tease:
  • :vent:
  • :win:
  • :work:
  • :good:
  • :shit:
  • :bomb:

Based on Fluidity© 1998-2007 Roy Hiu-yeung Chan