July 26, 2006

Create GUI dialogs for GNOME and KDE

Author: JT Smith

We have already discussed how you can create text user interfaces for command-line tools using dialog. This time, we are going to look at Zenity and KDialog, which allow you to create GNOME- and KDE-specific front ends for scripts.


Because Linux developers have made great strides in improving Linux's desktop interfaces and GUI applications, users have had no reason to focus on the command line. New users especially tend to be wary of the command-line interface (CLI) and favor the graphical environments (GUI).

After recognizing the importance of front ends, many developers started writing tools that could be used to easily create front ends for programs. Initially there was Dialog, which could be used to create more user-friendly text interfaces for command-line tools. As the Linux GUI userbase grew, developers created GUIs such as Xdialog, gdialog, and KDialog.

Zenity evolved from gdialog to create Gtk-specific front ends, and can be used to display GTK+ dialog boxes. Its usage and options are similar to those of gdialog, but, as its home page claims, it has a cooler name and is easier to use. Front ends created with Zenity have the same look and feel as those of other graphical applications under GNOME.

Similarly, KDialog creates KDE-specific dialog boxes that have the same look as other tools under KDE.

Zenity radio list

If you have both GNOME and KDE, you can run KDialog from within GNOME, but the front ends so made won't be exactly like the ones in GNOME. Specifically, the color scheme and fonts are the same, but the layout of the dialog box is different. You can test this by running kdialog --getopenfilename . and zenity --file-selection and comparing the dialog boxes with the open file box of gedit.

Using Zenity

Zenity is part of the default GNOME package, so you already have it installed on your system. You can get the latest RPM from RPMforge, or the Debian package from Debian's repository. Run zenity --question to test it. This displays a dialog box with the default message "Are you sure you want to proceed?"

In the previous article we discussed creating basic menus, checklists, and file selection. Let's see how we can do these with Zenity.

To create a basic menu, you need to use the --list option along with either the --checklist or the --radiolist option. Other options, such as --title, have the same usage as dialog and Xdialog. For example, to create a radio list, use:

zenity --title "This is a list" --text "Please choose a fruit" --list --radiolist --column "Buy this" --column "Fruit" False Mango False Pineapple False Grapes True Strawberries

Notice the two --column switches. They are a must. You cannot have a list without the two columns. That is, you cannot create a list with just one or no columns. The first column shows a checkbox or a radio button, depending on what kind of list you wish to create, and the other lists the available options.

Also, you need to specify the False or True flag before the options. This is different from dialog/Xdialog, where On or Off was written after the options. If you don't specify a title, the default "Select items from the list" is displayed. The default text message is "Select items from the list below."

When you run this code, the selected fruit is displayed on the screen, because Zenity writes the output to standard display by default. To save the selection to a text file, simply redirect the output to a file using > filename at the end of the command.

As with other tools in the dialog family, there are several types of dialog boxes to choose from. The man page describes all the available dialog boxes and their options.

You can use the --calendar option to display a calendar. When used without any other options, such as --month, --day, and --year, it displays the current day and month. All options expect an integer value, so the year 1985 is 1985, Wednesday is 4, and so on. Be careful when specifying the year. If you write 1985 as 85, all days are moved up by one. That is, September 2, 1985, which was a Monday, becomes Sunday if the year is specified as 85, because it actually calculates for 2085.

The --width and --height options can be used to define the dimensions of the dialog box. These options, however, can't be used with all dialog types supported by Zenity.

To create a file selection dialog box we use --file-selection. Here's a simple script to edit a text file using the file selection dialog.


zenity --question --text "Would you like to edit a file?"
if [ $? = 0 ];
 SELECTED_FILE=`zenity --file-selection`
 zenity --text-info --width=560 --editable --filename=$SELECTED_FILE > $SELECTED_FILE.edited
 echo "Saved $SELECTED_FILE as $SELECTED_FILE.edited"
 zenity --text-info --filename=$SELECTED_FILE.edited

The $? variable stores the return value. It is 0 if you press Ok and 1 if you press Cancel. Once you select a file, its path is stored in the SELECTED_FILE variable. The --text-info option enables us to view the file. The --editable option makes the file editable. When you are through editing the files, the changes are stored in a new file.

Using KDialog

Most of KDialog's options are similar to those in dialog, but it offers far more basic options than Zenity and dialog. You can even decide if you want Yes/No or Ok/Cancel buttons in your dialog boxes.

You can make a message box with --msgbox. If you wish to display more than one line in the message box, you can use \n to signify a new line: kdialog --msgbox "This is first line. \n This is second line.". You cannot specify the dimensions of a msgbox. The width and height depend on the length and the number of lines of text.

With KDialog you can even create passive popups, which are useful when you wish the message box to disappear after a certain amount of time or when the user clicks on the popup box. For instance, you can spawn a dialog that will last for five seconds using kdialog --title "A passive popup" --passivepopup "Will be visible for 5 seconds" 5. The box appears on the top left corner of the screen -- you can't change this behavior. After the five seconds elapse, it disappears.


In certain situations, such as copying files from one location to another, the warning box needs to have Yes/No options, while Continue/Cancel options are required when the user chooses to delete files. The --warningyesno or --warningcontinuecancel options allow you to do those.

Use the --inputbox dialog box if you want the user to insert some text. The string entered is displayed on standard output. To display a dialog box where the user can enter a name, use kdialog --title "An input dialog" --inputbox "Enter your full name:" . You can also display a default text string in the input box with kdialog --title "An input dialog" --inputbox "Enter your full name:" "John Doe"

Much like Zenity's --text-info dialog box, the --textbox can be used to display files. It requires you to specify a filename, and you can optionally specify the width and height of the display box, like so: kdialog --textbox file_selection.sh 300 150

To open a file selection dialog box, use the --getopenfilename dialog box. It requires a start directory position. To open the current directory you run the command kdialog --getopenfilename . where . represents the current directory. Replacing the . with /etc would open the /etc directory.

Optionally, you can use filter arguments to restrict what types of files are displayed. kdialog --getopenfilename /home/linuxlala/Desktop/me/ "image/png text/plain" would display only PNG and text files. You can even use wildcards to restrict files, such as *.mp3 in place of the MIME filter arguments.


KDialog seems far more helpful in terms of the basic options it provides. Other than that, both tools work flawlessly out of the box and require almost no getting used to. A basic shell scripting knowledge is a must, though, if you wish to create front ends for your favorite commands.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.

Click Here!