Text-based menus and information pages
Whip It Up
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.
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.
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
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 ./build
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 0
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
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
(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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.