Create GUI dialogs in one line of code
Finding a Little Zen
The Zenity command-line utility lets you create simple dialog boxes with your own data or with the output of utilities and applications.
Zenity is a command-line GUI creator that has been around since 2012 and is pre-installed on most versions of Linux, including Raspberry Pis. Zenity isn't designed to be a high-level GUI development tool, but if you just need some basic scripting with dialogs, then Zenity might be a perfect fit.
I was amazed that in one line of Bash code I was able to show:
- stats in a message dialog
- a web page in a dialog
- CSV data or SQL queries in a list dialog
After looking at slightly more complex applications, I found I was able to create:
- a progress bar with dynamic values (about seven lines)
- a form to insert user data into an SQL database (about eight lines)
- a four-button dialog to control a Rasp PI Rover (~20 lines)
In this article, I introduce Zenity with some examples.
Getting Started
The Zenity [1] command-line utility is supported on Linux, macOS, and Windows. Zenity can display calendar, color selector, file selector, form, list, message, notification, progress, scale, and text dialogs.
The Zenity message dialog can be used like a Bash echo statement; for example, to show an information message, enter:
zenity --info --text="Some text" --title="My Title"
Information from command-line tools and utilities can be passed to Zenity. For example, the instantaneous CPU idle time from the top
utility can be parsed and passed to a Zenity dialog (Figure 1):
zenity --info --text=$(top -n 1 | grep %Cpu | awk '{print $8}') --title="CPU Idle Time"
Text font and size can be modified in message dialogs in Pango markup language syntax [2]. Pango is similar to HTML, and the <span>
… </span>
set of tags is typically used to encode font and color definitions (Figure 2):
zenity --warning --text=' HIGH Temperature' --title="HDD Check"
For scripting applications that need to pass more information to users, text-info
dialogs can pass text files and URL links (Figure 3):
zenity --text-info --title="Background Reading" --html --url="https://developer.gnome.org" --checkbox="I read it...and I'm good to go"
For dialogs with OK and Cancel buttons, Zenity returns a
to confirm and a 1
to cancel. Zenity will not process JavaScript on a web page.
Simple dialogs like the info, warning and error dialogs will only have an OK button. All the other dialogs will have Cancel buttons, as well. The button text can be changed with the --ok-label
and the --cancel-label
options. More buttons can be added with the --extra-button
option.
Dynamic Values
A Zenity progress dialog can show dynamic updates with scripts that use subshells. A subshell is configured with parentheses. Step executions within the subshell are paused by sleep statements.
The following code shows a three-step example of a subshell with a Zenity progress dialog (Figure 4):
( echo "33"; echo "# 1/3 done" ; sleep 5; echo "66"; echo "# 2/3 done" ; sleep 5; echo "100";echo "# Finished" ) | zenity --progress --title="3 step test"
When a step outputs a value, the progress bar updates. The text on the progress dialog is changed by outputting a text string that starts with a #
character.
Listing 1 and Figure 5 show an example of current seconds with the progress bar rescaled from 0 to 60.
Listing 1
Scaled Progress Bar
#!/bin/bash # show_seconds.sh - progress dialog to show seconds echo "Press [CTRL+C] to stop..." ( while :; do echo "# $(date +'%S')" # Scale 0-60 to 0-100 echo "$(date +'%S')*100/60" | bc sleep 1 done ) | zenity --progress --title="Show Time in Seconds"
The Bash while
statement lets you show dynamic values continuously until the Cancel button is pressed.
CSV Data in List Dialogs
For simple text files and known datasets, the list
dialog works quite well. The dialog expects the data to be sequential. The following code creates a two-column example (Figure 6) with inline data:
zenity --list --title="2 Column Example" --column="Month" --column="Sales" Jan 100 Feb 95 Mar 77 Apr 110 May 111
To pass a data file (CSV or text) into Zenity, the text needs to be reformatted. The tr
command can replace CSV field separators like commas with a newline (\n) character (Listing 2).
Listing 2
Reformatting Data
$ cat pidata.csv 10:00,12,running 10:20,14,stopped 10:30,13,running $ cat pidata.csv | tr ',' '\n' 10:00 12 running 10:20 14 stopped 10:30 13 running
The output can then be passed to a Zenity list with column headings (Figure 7):
cat pidata.csv | tr ',' '\n' | zenity --list --title="Pi Data" --column="Time" --column="Temp" --column="Pump"
Once you have some Zenity and Bash basics down, you can start doing more advanced operations, such as:
awk -F "\"*,\"*" '{print $3 "\n" $1}' pidata.csv | zenity --list --column="field3" --column="field1"
This one-line example uses Awk to parse specific data (fields 1 and 3) in the CSV file.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.