热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shell编程zenity_如何在Linux上使用Zenity制作简单的图形Shell脚本

shell编程zenityZenityaddsgraphicalinterfacestoshellscriptswithasinglecommand.Shellscriptsare
shell编程zenity

shell编程zenity

Zenity adds graphical interfaces to shell scripts with a single command. Shell scripts are a great way to automate repetitive tasks, but they’re normally confined to the terminal — Zenity brings them out of the terminal and onto your desktop.

Zenity只需一个命令即可将图形界面添加到Shell脚本。 Shell脚本是自动执行重复任务的好方法,但通常只限于终端机-Zenity将它们带出终端机并带到您的桌面上。

We’ve given an introduction to shell scripting in the past. You don’t have to be a programmer to get started with shell scripts — they require little more than knowledge of Linux terminal commands.

过去,我们已经介绍了Shell脚本 。 您无需成为程序员即可开始使用Shell脚本-它们只需要了解Linux终端命令即可。

获得热情 (Getting Zenity)

Zenity comes with Ubuntu by default. If you use an Ubuntu derivative, Such as Kubuntu, you may have to install it manually with the following command:

Zenity默认随附于Ubuntu。 如果使用Ubuntu衍生产品(例如Kubuntu),则可能必须使用以下命令手动安装它:

sudo apt-get install zenity

sudo apt-get install zenity

Zenity is a part of GNOME, so it should already be included on Linux distributions that use the GNOME desktop. Check your package manager for the zenity package if you don’t have it.

Zenity是GNOME的一部分,因此它应该已经包含在使用GNOME桌面Linux发行版中。 如果您没有zenity软件包,请检查您的软件包管理器。

使用Zenity (Using Zenity)

You can play around with Zenity from the terminal. Let’s say you want to create an error window when a problem occurs with your shell script. Here’s an example command you could use:

您可以在终端上玩Zenity。 假设您要在Shell脚本出现问题时创建一个错误窗口。 这是您可以使用的示例命令:

zenity –error –title=”An Error Occurred” –text=”A problem occurred while running the shell script.”

zenity –error –title =“发生错误” –text =“运行shell脚本时发生问题。”

Run the command and you’ll see a window with the message.

运行命令,您将看到一个带有消息的窗口。

Put this single command into your shell script at the correct place and you’ll have a graphical error message. You could also use variables to include more information about the error.

将此命令放在正确的位置的Shell脚本中,您将收到图形错误消息。 您还可以使用变量来包含有关该错误的更多信息。

Let’s say you want to ask a yes or no question. You could use a command like this one:

假设您要询问是或否的问题。 您可以使用如下命令:

zenity –question –title=”Query” –text=”Would you like to run the script?”

zenity –问题–title =“查询” –text =“您要运行脚本吗?”

You can catch the yes or no response in your shell script and perform different commands based on which button the user clicks.

您可以在shell脚本中捕获是或否响应,并根据用户单击的按钮执行不同的命令。

There’s also a text entry dialog:

还有一个文本输入对话框:

zenity –entry –title=”Favorite Website” –text=”What is your favorite website?”

zenity –entry –title =“最喜欢的网站” –text =“您最喜欢的网站是什么?”

Catch the user’s input in a shell script and you could store it as a variable.

在shell脚本中捕获用户的输入,您可以将其存储为变量。

There’s also a file picker, calendar, and other types of dialogs. For a full list of dialog types and their options, consult Zenity’s manual page.

还有一个文件选择器,日历和其他类型的对话框。 有关对话框类型及其选项的完整列表,请参阅Zenity的手册页 。

示例脚本 (An Example Script)

Let’s try using Zenity to create a simple graphical shell script. With just three commands, we can create a graphical timer program:

让我们尝试使用Zenity创建一个简单的图形化shell脚本。 仅需三个命令,我们就可以创建一个图形计时器程序:

#!/bin/bash # This script asks the user for a time, waits the specified amount # of time, and shows an alert dialog.

#!/ bin / bash#此脚本询问用户一段时间,等待指定的时间#,并显示警报对话框。

TIME=$(zenity –entry –title=”Timer” –text=”Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.”)

TIME = $(zenity –entry –title =“ Timer” –text =“输入计时器的持续时间。\ n \ n使用5s表示5秒,10m表示10分钟,或2h表示2小时。”)

sleep $TIME

睡$ TIME

zenity –info –title=”Timer Complete” –text=”The timer is over.\n\n It has been $TIME.”

zenity –info –title =“计时器已完成” –text =“计时器已结束。\ n \ n已经是$ TIME。”

We’re using some extra tricks here. We get the value of the TIME variable from the first zenity command and feed it to the sleep command. We’re also using /n to create new lines of text in the zenity dialogs.

我们在这里使用一些额外的技巧。 我们从第一个zenity命令获取TIME变量的值,并将其输入到sleep命令。 我们还使用/ n在zenity对话框中创建新的文本行。

After saving the shell script and running the chmod +x command on it to give it executable permissions, we can launch it.

保存shell脚本并对其运行chmod + x命令以赋予其可执行权限后,我们可以启动它。

Enter a duration and the script will use the standard sleep command to count down in the background. When the sleep command’s timer finishes, the script will display the zenity info message.

输入持续时间,脚本将使用标准的sleep命令在后台倒数。 当sleep命令的计时器结束时,脚本将显示“ zenity info”消息。

You could create a desktop or panel shortcut for this script and run it without even touching the terminal.

您可以为该脚本创建桌面或面板快捷方式,并在不触摸终端的情况下运行它。



This is just scratching the surface of what you could do with zenity; you could use it to make much more complicated programs. If you’re looking for more information on shell scripting, check out our guide to using for loops in shell scripts.

这只是在摸索您可以使用Zenity做的事情的表面; 您可以使用它来制作更复杂的程序。 如果您正在寻找有关shell脚本的更多信息,请查看我们的shell脚本中使用for循环的指南。

翻译自: https://www.howtogeek.com/107537/how-to-make-simple-graphical-shell-scripts-with-zenity-on-linux/

shell编程zenity



推荐阅读
author-avatar
HikariFocus_695
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有