nity uses to call them are on the left witha picture of the dialog box next to it.As you can see, the selection includesinput fields. Bash reads these fieldswhen you click
field is anexample of this. To pass a numeric valueto Bash, use the slider (
control lists information and supportssorting if you click a field name.A progress indicator is useful wher-ever you need to monitor progress; forexample, you could use one to show theprogress of a search. But I’ll look at thebasics first.To label a window, you generally wantto use the
keyword. Listing 2shows how to let the user select a valuebetween
) and assign a default of
). This gives you a dialoglike that shown in Figure 1.Of course, you can feed only a limitednumber of parameters to a slider:
has a separate
sec-tion outlining the options that work with
fieldonly supports text defini-tions and new lines, for ex-ample. Some types need tobe combined with othercommands to be effective.The command
ls | zenity
lists the files in a directoryand sorts them alphabeti-cally when you click
(Figure 2).Although Zenity willbuild interesting graphicalmenus for you, it does nottake the task of designingprogram logic off your hands. Therefore,you need an If condition to tell the scriptwhat to do when you click
. Ingeneral, Zenity and KDialog define twointernal return values for dialog boxes: Apositive result (
and a neg-ative result (
. A third result– such as
from KDialog – wouldreturn
. You can create conditionalstatements by querying these return val-ues.
On Top of KDE
KDialog users canfollow a similar ap-proach to creategraphical dialogboxes. Table 2 showswhich commandscreate which ele-ments. KDialog hasa couple of addi-tional dialogs thatZenity does nothave. For example,to have the user con-firm a file deletion,enter:
"Do you really want
to remove this file?"
The question appearsin a dialog box with
buttons.If you want to labelthe field, add a title(Figure 3):
kdialog ‑‑title "Yes
or No"‑‑yesno "Do you
really want to
remove this file?"
In contrast to Zenity,KDialog complainsif you just type
without adding text – theprogram does not generate default la-bels.If you enter text in a field, it is typi-cally displayed on the console, which isthe standard output device, but you alsocan pipe the output into a file – for ex-ample,
(Listing 3, line 1). Alter-natively, you can pass the results to avariable (Listing 3, lines 2 and 3).In addition to the messages mentionedhere, the
switch makes surethat KDialog does not continually repeat
01 kdialog ‑‑passivepopup "That really wasn't necessary..." 502 kdialog ‑‑getopenfilename . " *.mp3 | MP3‑Dateien"
Listing 4: Opening a Specific File Type
01 kdialog ‑‑inputbox "Please enter any text here:" > file1.txt02 variable=$(kdialog ‑‑inputbox "Please enter more text here:")03 echo $variable
Listing 3: Passing the Results to a Variable
Figure 4: The --passivepopup option lets you display a short mes-sage so that the user sees the pop-up without losing the currentfocus.Figure 3: KDialog will quicklyset up a Yes/No dialog.Figure 2: Click Show Directoryto list the files.
Zenity and KDialog