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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
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.