Text-based menus and information pages

Whip It Up

© Lead Image © PazMadrid, morguefile.com

© Lead Image © PazMadrid, morguefile.com

Article from Issue 270/2023

Whiptail interfaces add menus and information pages to your Raspberry Pi projects.

Text interfaces can be extremely useful for Windows or secure shell (SSH) clients that need to connect to your Linux or Raspberry Pi systems. One of the simplest options for creating text interfaces is Whiptail [1], which comes preinstalled on Raspbian and many Linux systems. The Raspberry Pi configuration tool, raspi-config (Figure 1), is a good illustration of how Whiptail can be used.

Figure 1: The Raspberry Pi configuration tool raspi-config uses Whiptail.

In this article, I introduce Whiptail with three small projects. The first creates a menu interface for some common Linux diagnostic tools, the second controls Raspberry Pi output pins, and the final uses Python to display Pi sensor data.

Getting Started

If your system does not have Whiptail, you can install it on Raspbian/Debian/Ubuntu machines with the command:

sudo apt install whiptail

A simple Whiptail test shows the date/time in a message box:

whiptail --title "Time/Date" --msgbox "$(date)" 0 0

This statement clears the terminal screen and shows a default background with a message box centered in the window (Figure 2). The last two parameters in the statement define the height and width of the dialog. The 0 0 at the end of the last line autosizes the message box.

Figure 2: Whiptail message box.

The Whiptail utility supports a variety of controls, such as: checklist, gauge, infobox, inputbox, menu, msgbox, passwordbox, radiolist, textbox, and yesno.

A Menu of Diagnostic Tools

Menuing is one of most useful features in Whiptail. A main script can present a top-level menuing interface that links to submenus, information pages, or applications. The syntax for the menu options is:

[...] --menu  <height> <width> <listheight> [<tag> <item>]

The <item> parameter is the text string shown on the menu line. The <tag> parameter is the variable passed when a menu is selected; this parameter is typically a number, but you can also use strings. In my menuing example, I will be passing the diagnostic utility name as the tag.

Listing 1 is a Whiptail menuing script that shows three common Linux diagnostic tools, df (disk space usage), vmstat (virtual memory statistics), and lsusb (list USB devices). For this example, a fourth menu line exits the script (Figure 3). The --menu option is defined to autosize with four menu items (0 0 4; line 9). Each menu has a line description and a tag that is passed when that line is selected.

Listing 1

Whiptail Menus

01 #!/bin/bash
02 #
03 # wmenu.sh - Wipetail Menu example with diagnostic tools
04 #
06 while true
07 do
08 # Create a whiptail menu, pass the tool name if selected
09 selection=$(whiptail --title "Diagnostics" --nocancel   --menu "Select Tool:" 0 0 4    "df" " Disk Space"    "vmstat" " Memory Stats"    "lsusb" " USB Devices"    "Exit" " Close and Exit" 3>&1 1>&2 2>&3 )
11   # Do an action for the selected menu item
12   case $selection in
13     "df" )
14       whiptail --title "Disk Space" --msgbox "$(df -h)" 0 0
15       ;;
16     "vmstat" )
17       whiptail --title "Memory Stats" --msgbox "$(vmstat --stats)" 0 0
18       ;;
19     "lsusb" )
20       whiptail --title "USB Devices" --msgbox "$(lsusb)" 0 0
21       ;;
22     "Exit" )
23       exit
24       ;;
25   esac
26 done
Figure 3: This Whiptail menu shows diagnostic data.

For menuing applications the Whiptail utility requires a set of redirection statements, ( 3>&1 1>&2 2>&3 ) at the end of the main command. These redirections are a Bash trick to swap standard output, (file 1, stdout), and standard error (file 2, stderr). By adding these statements Whiptail has a clean, error free, mechanism for user input and menu output.

This menu example only uses an Ok button, the Cancel button is disabled by the --nocancel parameter (line 9). In the next example the Cancel button will exit the script.

Raspberry Pi Radiolist Project

The goal of the Raspberry Pi radiolist project is to create a simple user interface that controls general purpose input/output (GPIO) pins. For my setup I am using a Pimoroni Explorer HAT (hardware attached on top) that has four LEDs; however, other hardware arrangements could also be used.

The first step for this project is to install wiringPi [2], so that GPIO pins can be accessed and controlled from Bash scripts. This utility is installed on a Raspberry Pi with:

sudo apt-get install git-core
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
git pull origin

The wiringpi library includes the gpio command-line utility that you can use to read, write, toggle, and set the mode of GPIO pins. My hardware arrangement has four LEDs on pins 0, 2, 7, and 21 that I need to set up for output:

# Set the LED pins as outputs
gpio mode 7 output
gpio mode 0 output
gpio mode 2 output
gpio mode 21 output

Once the GPIO pins are set up as outputs, I can toggle their states manually:

# Toggle the output state on pin 7
gpio toggle 7

The syntax of a radiolist is similar to a menu:

--radiolist  <height> <width> <list-height> [ <tag> <item> <status> ]

The only difference with a checklist is that you can indicate which entry is currently selected by setting its status to on. The default radiolist has two buttons, an Ok, which returns a   when selected, and a Cancel button, which returns a 1.

The Raspberry Pi radiolist script (Listing 2) shows a list of GPIO pins the user can toggle (Figure 4). A while statement loops as long as the Ok button is selected (lines 9 and 14). The radiolist is defined to show a GPIO pin in the <tag> parameter, and the <item> parameter displays the LED number, color, and physical pin number (lines 11). An if statement checks a GPIO pin (line 17) and then passes a valid pin number to the gpio toggle statement (line 19).

Listing 2

Whiptail Radiolist

01 #!/usr/bin/bash
02 #
03 # wradio.sh - toggle a GPIO output pin
04 #
05 OK=0
06 response="$OK"
08 # Cycle OK entered, Enter Cancel to exit
09 while [ "$response" == "$OK" ] ; do
11   thepin=$(whiptail --title "Toggle GPIO Pins"     --backtitle "Raspberry PI GPIO"     --radiolist "Select Pin:" 0 0 4       7 "GPIO_7 - LED 1 (BLUE) physical pin 7 " off       0 "GPIO_0 - LED 2 (YELLOW) physical pin 11" off       2 "GPIO_2 - LED 3 (RED) physical pin 13" off       21 "GPIO_7 - LED 4 (GREEN) physical pin 29 " off     3>&1 1>&2 2>&3 )
13   # Get the response from the radio dialog
14   response=$? ; # Get the output from the dialog
16   # Check if a radio item returned a value
17   if [ ${#thepin} > 0  ] ; then
18     # Toggle the selected GPIO pin number
19     gpio toggle $thepin
20   fi
20 done
Figure 4: Raspberry Pi radiolist project.

This project could be enhanced with a checklist box that shows the present status of all GPIO pins; then, rather than setting one pin, multiple pins could be set or reset in a single pass.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Julia on the Pi

    Create GUIs and a web app that connects to sensors.

  • Xonsh

    Create lightweight Raspberry Pi scripts with Xonsh, a Python shell that lets you write scripts in Python with Bash commands mixed in.

  • Bash Web Server

    With one line of Bash code, you can create a Bash web server for quickly viewing the output from Bash scripts and commands.

  • Gnuplot

    Use Gnuplot with command-line utilities.

  • Go on the Rasp Pi

    We show you how to create a Go web app that controls Raspberry Pi I/O.

comments powered by Disqus
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.

Learn More