linux下gtk+图形界面--zenity的使用练习

时间:2022-05-27 17:07:13

使用zenity的主要目的是:

1)使用起来简单方便,大多数linux都已经默认安装,无需再进行安装;

2)程序进行到某一步骤时发出警告,或者发生错误时else第二选择生效,运行zenity,弹出出错对话框,方便提示,以及阻止程序向下继续运行;

3)用于交互式输入,给出entry对话框,赋值给对应变量,这方面主要方便对命令不熟悉,喜欢图形化操作的使用;

4)其它方面,想怎么用就怎么用吧

Dialog options   #对话框选项

--calendar #日历对话框
Display calendar dialog

--entry #输入对话框
Display text entry dialog

--error #错误提示框
Display error dialog

--file-selection #文件选择对话框
Display file selection dialog

--info Display info dialog #信息提示对话框

--list Display list dialog #列表显示对话框

--notification #注意对话框
Display notification icon

--progress #进度提示框
Display progress indication dialog

--question #问题对话框
Display question dialog

--text-info #文本信息对话框
Display text information dialog

--warning #警告提示框
Display warning dialog

--scale #比例显示框
Display scale dialog

--color-selection #颜色对话框
Display color selection dialog
1、calendar

#!/bin/bash

zenity --calendar --title="hello test" --width='100' --height='50' --timeout='100' --text='about me' --day='10' --month='9' --year='2013' --date-format='%A%d%m%y'

得到如下界面:

linux下gtk+图形界面--zenity的使用练习

就是进行一个指定日期的显示,以及可以自己定义标题,提示信息和对话框大小。

2、entry

#!/bin/bash

ps=`zenity --entry --title="hello test" --width='200' --height='180' --timeout='100' --text='about me' --entry-text='it is a entry text' `
得到如下图:

linux下gtk+图形界面--zenity的使用练习

这样就可以出现一个输入提示对话框,里面的内容是预设了的,也可以直接设置为空,让用户自己输入,然后就可以将输入的值赋给对应的ps了,这样就可以利用输入的信息进行下一步其它的运算了。

不显示参数 --hide-text

#!/bin/bash

ps=`zenity --entry --title="hello test" --width='200' --height='180' --timeout='100' --text='about me' --hide-text='' `
echo $ps
这样就可以让输入不显示明文,如下图所示:

linux下gtk+图形界面--zenity的使用练习

当然 timeout是超时机制。

3、error dialog

#!/bin/bash

zenity --error --text="hello test" --width='200' --height='180' --timeout='10'

错误提示框如下:

linux下gtk+图形界面--zenity的使用练习

4、file selection

#!/bin/bash

zenity --file-selection --title="choose a file" --filename="/home/loongson/" --multiple --width='200' --height='180'
得到如图所示,注意标题已修改

linux下gtk+图形界面--zenity的使用练习

还有多项选择功能,覆盖功能,不再演示;

5、information opinions

#!/bin/bash

zenity --info --text="hello test" --title='inforation opinions' --no-wrap --width='200' --height='180' --timeout='100'
得到如下图所示:

linux下gtk+图形界面--zenity的使用练习

6、list opinions

#!/bin/bash

zenity --list --text="text for list" --title="choose a file" --column="one" --column="two" --column="three" --multiple --separator="|" --radiolist --print-column='3' --width='200' --height='180'
linux下gtk+图形界面--zenity的使用练习

7、notification

#!/bin/bash

zenity --notification --text="hello test" --width='200' --height='180' --timeout='100' --window-icon='/home/loongson/zenity_test/error.png'

运行后一直没有跳出对话框,不明白。

8、progress

#!/bin/bash

function a() {
echo "10";sleep 1
echo "#updating mail logs";sleep 1
echo "20";sleep 1
echo "#wait...";sleep 1
echo "100";sleep 1
echo "#ok";sleep 1

}

a | zenity --progress \
--text="scaning mail logs" \
--percentage=0 \
--title="update"

得到如下对话框:

linux下gtk+图形界面--zenity的使用练习


9、question

#!/bin/bash

zenity --question --text="hello test" --no-wrap --ok-label 'YES' --cancel-label 'NO' --width='200' --height='180' --timeout='100'
得到如下对话框,需要注意的是选项可以自己定义“YES”或“NO”的内容

linux下gtk+图形界面--zenity的使用练习

10、text opinion

#!/bin/bash

zenity --text-info --editable --filename='/home/loongson/zenity_test/test.sh' --width='200' --height='180' --timeout='100'
可以对文件夹的内容进行显示

另外注意选项是“

 --text-info
”,不是--text

linux下gtk+图形界面--zenity的使用练习linux下gtk+图形界面--zenity的使用练习

一个是空白可编辑,一个是文件含内容,进行浏览

11、waring

#!/bin/bash

zenity --warning --text="text for warning" --title="ATTENTION" --width='200' --height='180'

进行警告提示如下:

linux下gtk+图形界面--zenity的使用练习

12、scale

#!/bin/bash

zenity --scale --text="text for scale" --title="SCALE" --value='1' --min-value='0' --max-value='100' --step='1' --print-partial --width='200' --height='180'
linux下gtk+图形界面--zenity的使用练习
13、color

这个在机器上没有实现。不知道颜色如何设置,提示不支持。


官方自带的例子如下:

EXAMPLES
       Display  a  file  selector  with  the  title  Select a file to remove. The file
       selected is returned on standard output.

              zenity  --title="Select a file to remove" --file-selection

       Display a text entry dialog with the title Select Host and the text Select  the
       host  you  would  like  to flood-ping. The entered text is returned on standard
       output.

              zenity  --title "Select Host" --entry --text "Select the host you  would
              like to flood-ping"

       Display  a  dialog,  asking Microsoft Windows has been found! Would you like to
       remove it?. The return code will be 0 (true in shell) if OK is selected, and  1
       (false) if Cancel is selected.

              zenity   --question  --title "Alert"  --text "Microsoft Windows has been
              found! Would you like to remove it?"

       Show the search results in a list dialog with the title Search Results and  the
       text Finding all header files....

              find  .  -name  '*.h'  |  zenity  --list --title "Search Results" --text
              "Finding all header files.." --column "Files"

       Show an icon in the notification area

              zenity --notification  --window-icon=update.png  --text  "System  update
              necessary!"

       Display  a  weekly shopping list in a check list dialog with Apples and Oranges
       pre selected

              zenity --list --checklist --column "Buy"  --column  "Item"  TRUE  Apples
              TRUE Oranges FALSE Pears FALSE Toothpaste

       Display  a progress dialog while searching for all the postscript files in your
       home directory

       find $HOME -name '*.ps' | zenity --progress --pulsate