Zenity create a form dialog

by Daniel Pham
Published: Updated:
This entry is part 8 of 13 in the series GUI for bash script using Zenity

In this article, we will use zenity to create a form dialog. I think this is a very interesting feature in zenity. When creating the GUI, creating a form is indispensable. And with just a few lines of zenity code, you can create a form with basic functionality.

Create a form dialog

To create a form dialog, you use the --forms option in zeniy.

zenity --forms

When you type the command above, a basic dialog will open. However, you will not be able to enter any data because the form is empty at this time.

Zenity create a form dialog
Create a form dialog

In the next article, we will talk about the options of --forms. When using these options, you can create a complete form to use.

Forms options

The table below shows the options of the form in zenity.

OptionMeaning
--add-entryAdd a new entry in form dialog. You can add many entry as you want.
--add-passwordAdd a new password entry in form dialog. The text you type in this entry will be hide.
--add-calendarAdd a new calendar in form dialog. When you select a date in the form, it returns the date in the command window.
--textSet the dialog text. You can add a note to your form via this option.
--separatorSet output separator character. Default is ‘|’, you should put it in parentheses.
--forms-date-formatSet the format for the returned date. This option formats the results when you use –add-calendar. This option use strftime function, for example %A %d/%m/%y.

The --forms-date-format option uses the strftime function, which you can read under the link I mentioned in a zenity calendar article.

In the example below, I will use all of the options above to create a form dialog.

zenity --forms --add-entry=Username --add-password=Password --add-calendar=Date --text="WriteBash.com Demo Form" --separator=';' --forms-date-format=%d/%m/%Y
Zenity create a form dialog
Create a form dialog with full options

When you enter the information, select the date on the calendar. A result line will return as the image below.

Zenity create a form dialog
The values return

The stream of data returned in the image above, you can use it in your scripts.

Create a form dialog with bash script

Content script below is used in part from documents Zenity, you can copy the script below and use on your computer.

Read moreMake bash script run as a service in CentOS 7.

#!/bin/bash
# Script author: Danie Pham
# Script site: https://devopslite.com
# Script date: 26-01-2018
# Script use to create a form dialog by using Zenity

# Function create a form
f_create_form () {
    # This content is reused from the Zenity document
    zenity --forms --title="Add Friend" \
        --text="Enter information about your friend." \
        --separator="," \
        --add-entry="First Name" \
        --add-entry="Family Name" \
        --add-entry="Email" \
        --add-calendar="Birthday" >> addr.csv

    case $? in
        0) echo "Friend added." ;;
        1) echo "No friend added." ;;
        -1) echo "An unexpected error has occurred." ;;
    esac
}

# Main function
f_main () {
    f_create_form
}
f_main

exit

Conclusion

I personally think this is a powerful option of zenity. With just a few lines of code, you can create a form dialog. When combined with the bash script, you can make it a complete program.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Zenity create a progress dialogNext part: Zenity create a scale dialog »»

You may also like

Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Julio Neves
Julio Neves
Guest
5 years ago

An example using the last implementations –add-list, –list-values and –show-header
zenity –forms \
–text “Formulário com lista de duas colunas” \
–add-entry “Nome” –add-list=”Lista dupla” \
–column-values “Núm|Descritivo” \
–list-values=”1|Primeira Linha|2|Segunda Linha\
|3|Terceira linha|” –show-header

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.