zenity dialog window with two buttons but no text entry

Remi.b picture Remi.b · Jun 23, 2016 · Viewed 9.8k times · Source

I would like to create a zenity dialog window with two buttons as only user input.

The following creates a window with two buttons but with a space for a text entry

zenity --entry --title="" --text "Choose A or B" --ok-label="B" --cancel-label="A"

The following creates a window with one button only

zenity --info --title="" --text "Choose A or B" --ok-label="B"

Answer

Nexus picture Nexus · Mar 23, 2018

At least recent versions of zenity have an --extra-button flag.

Combining the value of the exit code and the content of stdout, the code can figure out what the user did.

For example:

while true; do
  ans=$(zenity --info --title 'Choose!' \
      --text 'Choose A or B or C' \
      --ok-label A \
      --extra-button B --extra-button C \
      --timeout 3)
  rc=$?
  echo "${rc}-${ans}"
done

Results would look like this:

# timeout
5-
# ESC key
1-
# A button
0-
# B button
1-B
# C button
1-C

Note that the above works similarly other dialogs, though certain combinations may be surprising. Be sure to experiment and handle all sorts of different user interactions.