我偶然发现可以显示带有 zenity 的组合框(测试版本:2.32.1)。请参阅以下代码:
#!/bin/bash
array=(a b c d e)
value=$(zenity --entry --title "Window title" --text "${array[@]}" --text "Insert your choice.")
Run Code Online (Sandbox Code Playgroud)
结果用以下 3 张图像说明:



我有两个问题:
是否有关于此功能的文档?我在zenity 文档中没有找到任何内容。
为什么我的数组的第一个值没有出现在组合框中?在上面的例子中,我的数组是(a b c d e),组合框只显示b c d e。
作为一种解决方法,我在数组中添加了一个值,例如(0 a b c d e).
小智 5
我认为您想用于--text-entry值数组,而不是--text(参考)。使用:
#!/bin/bash
array=(a b c d e)
value=$(zenity --entry --title "Window title" --entry-text "${array[@]}" --text "Insert your choice.")
Run Code Online (Sandbox Code Playgroud)
我看到下拉框的默认值预先填充了数组的第一个值以及所有可用值。
数组的第一个元素被 吃掉了--text。扩展后,您的 zenitiy 线如下所示:
zenity --entry --title "Window title" --text a b c d e --text "Insert your choice."
# Which zenity treats equivalent to
zenity --entry --title "Window title" --text a --text "Insert your choice." b c d e
Run Code Online (Sandbox Code Playgroud)
因此,您首先将文本设置为a,然后使用“插入您的选择”覆盖它。剩下的论点成为选择。
你想要的是:
zenity --entry --title "Window title" --text "Insert your choice." a b c d e
# Hence:
zenity --entry --title "Window title" --text "Insert your choice." "${array[@]}"
Run Code Online (Sandbox Code Playgroud)
这实际上被记录在案(可能不是在发布问题时,没有检查),不是在手册中,而是在zenity --help-forms :
$ LANG=en_US zenity --help-forms
Usage:
zenity [OPTION...]
Forms dialog options
--forms Display forms dialog
--add-entry=Field name Add a new Entry in forms dialog
--add-password=Field name Add a new Password Entry in forms dialog
--add-calendar=Calendar field name Add a new Calendar in forms dialog
--add-list=List field and header name Add a new List in forms dialog
--list-values=List of values separated by | List of values for List
--column-values=List of values separated by | List of values for columns
--add-combo=Combo box field name Add a new combo box in forms dialog
--combo-values=List of values separated by | List of values for combo box
--show-header Show the columns header
--text=TEXT Set the dialog text
--separator=SEPARATOR Set output separator character
--forms-date-format=PATTERN Set the format for the returned date
Run Code Online (Sandbox Code Playgroud)
所以:
$ LANG=en_US zenity --help-forms
Usage:
zenity [OPTION...]
Forms dialog options
--forms Display forms dialog
--add-entry=Field name Add a new Entry in forms dialog
--add-password=Field name Add a new Password Entry in forms dialog
--add-calendar=Calendar field name Add a new Calendar in forms dialog
--add-list=List field and header name Add a new List in forms dialog
--list-values=List of values separated by | List of values for List
--column-values=List of values separated by | List of values for columns
--add-combo=Combo box field name Add a new combo box in forms dialog
--combo-values=List of values separated by | List of values for combo box
--show-header Show the columns header
--text=TEXT Set the dialog text
--separator=SEPARATOR Set output separator character
--forms-date-format=PATTERN Set the format for the returned date
Run Code Online (Sandbox Code Playgroud)