Zenity create a list dialog

Share this post to your friends !

This entry is part 10 of 13 in the series GUI for bash script using Zenity
Zenity create a list dialog
5 (100%) 1 vote[s]

WriteBash - Today, i will guide you how to use zenity to create a list dialog. The list dialog is quite useful, so you can use it to create options in your program. Normally, you will see at programs during installation, they usually give you windows with a list of options. This article will help you use zenity to do that.

Create a list dialog

First, zenity uses the --list option to create a list dialog. However, unlike other options, if you only run command zenity --list, nothing will happen. We are creating a window with the values available for selection, you need to add the value in your command.

You can try running the following command:

zenity --list --column="Column name 1" --column="Column name 2" "Text row 1" "Value row 1" "Text row 2" "Value row 2"
zenity-create-a-list-dialog-01 Zenity create a list dialog
Zenity create a list dialog with basic options

In the above command, you can see the --column option to set the name for the columns in the dialog box, you can create as many columns as you want. In the above command, I created 2 columns. Therefore, each row should have two fields; Here we create each pair is Text row 1 – Value row 1 and Text row 2 – Value row 2. If it is a string then you write it in quotes, and if it is a number then you write normally.

By default, when you select a row in the dialog box, the value in the first column is returned. Here, the value returned is Text row 1 or Text row 2.

List dialog options

The table below lists the options that the --list supports. Note, when you use the --checklist option or the --radiolist option, each line must begin with TRUE or FALSE (this is the value that indicates whether or not the line is selected). And when using these two options, the --editable option does not work.

List dialog options
OptionMeaning
--column=columnSet the column headers will display in the list dialog. You can see the example above. As i said, you can create as many columns as you want.
--checklistSet the first column in the list dialog contains check boxes. You can see image below this table.
--radiolistSet the first column in the list dialog contains radio boxes. You can see image below this table.
--editableAllows you to edit values in row in the list dialog. Does not work when you use --checklist or --radiolist.
--separator=separatorSet what string is used when the list dialog returns the selected entries. Default character is “|“.
--print-column=columnDefault the first column will return value. You can use this option to print out special column or all columns. Value for this options is: 1, 2, … or ALL.

Okey, now i will create a list dialog with --checklist option.

zenity --list column=Check --column="Column name 1" --column="Column name 2" TRUE "Text row 1" "Value row 1" FALSE "Text row 2" "Value row 2" --checklist
zenity-create-a-list-dialog-02 Zenity create a list dialog
Zenity create list dialog with checklist option.

You see the row that starts with TRUE will be selected as soon as the window opens. For checklist, you can select one or more or all rows.

zenity --list column=Check --column="Column name 1" --column="Column name 2" TRUE "Text row 1" "Value row 1" FALSE "Text row 2" "Value row 2" --radiolist
zenity-create-a-list-dialog-03 Zenity create a list dialog
Zenity create list dialog with radiolist option.

For --radiolist options, you can select only one row in the dialog. You can try the remaining options and see how the results.

Bash script to create a list dialog

Copy below script to your computer and use it in the program as you want.

Read moreCreate a form dialog.

#!/bin/bash
# Script author: Danie Pham
# Script site: https://www.writebash.com
# Script date: 29-01-2018
# Script use to create a list dialog by using Zenity

# Function create a scale dialog
f_create_list () {
    zenity --list \
    --title="WriteBash.com demo create list dialog" \
    --text="Vote for this post" \
    --column="How useful" --column="Star rating" \
    "Not useful" 1 \
    "A little" 2 \
    "Okey" 3 \
    "Useful" 4 \
    "Very useful" 5
}

# Main function
f_main () {
    f_create_list
}
f_main

exit

Conclusion

This article, I gave quite a good example of the --list option in zenity. With this option, it looks very simple, but it is very powerful. You can use it in many cases.

Continue reading the series«« Previous part: Zenity create a scale dialogNext part: Zenity create a text entry dialog »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.