如何从 Linux 中的 bash 脚本显示 GUI 消息框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:20:10  来源:igfitidea点击:

How to show a GUI message box from a bash script in linux?

linuxbashscriptingubuntugtk

提问by Blorgbeard is out

I'm writing a few little bash scripts under Ubuntu linux. I want to be able to run them from the GUI without needing a terminal window to enter any input or view any output.

我正在 Ubuntu linux 下编写一些小的 bash 脚本。我希望能够从 GUI 运行它们,而无需终端窗口来输入任何输入或查看任何输出。

So far the only input required is a password for sudo - and gksudo handles that fine. But I haven't found an easy way to show a message box yet. Is there some kind of 'gkmessage' command available? I'd prefer something present in a default Ubuntu install, but I don't mind installing a new package if necessary.

到目前为止,唯一需要的输入是 sudo 的密码 - 而 gksudo 可以很好地处理。但是我还没有找到一种简单的方法来显示消息框。是否有某种“gkmessage”命令可用?我更喜欢默认 Ubuntu 安装中存在的东西,但我不介意在必要时安装一个新包。

采纳答案by Derek Park

I believe Zenitywill do what you want. It's specifically designed for displaying GTK dialogs from the command line, and it's available as an Ubuntu package.

我相信Zenity会做你想做的。它专为从命令行显示 GTK 对话框而设计,可作为Ubuntu 包使用

回答by Blorgbeard is out

I found the xmessagecommand, which is sort of good enough.

我找到了xmessage命令,它已经足够好了。

回答by Mark Harrison

Here's a little Tcl script that will do what you want. The Wish interpreter should be installed by default on Ubuntu.

这是一个小 Tcl 脚本,可以执行您想要的操作。Wish 解释器应该默认安装在 Ubuntu 上。

#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent

Call it like this:

像这样调用它:

myanswer=`gui-prompt "type your answer and press enter"`

回答by Steve Baker

There is also dialogand the KDE version kdialog. dialogis used by slackware, so it might not be immediately available on other distributions.

还有dialog和 KDE 版本kdialogdialog由 slackware 使用,因此它可能不会立即在其他发行版上可用。

回答by Jarek

Kdialog and dialog are both good, but I'd recommend Zenity. Quick, easy, and much better looking the xmessage or dialog.

Kdialog 和 dialog 都不错,但我推荐 Zenity。快速、简单且更好看的 xmessage 或对话框。

回答by Jarek

Zenityis really the exact tool that I think that you are looking for.

Zenity确实是我认为您正在寻找的确切工具。

or

或者

zenity --help

回答by Jim

The zenityapplication appears to be what you are looking for.

zenity应用程序似乎是你在找什么。

To take input from zenity, you can specify a variable and have the output of zenity --entrysaved to it. It looks something like this:

要从zenity获取输入,您可以指定一个变量并将zenity --entry的输出保存到其中。它看起来像这样:

my_variable=$(zenity --entry)

If you look at the value in my_variablenow, it will be whatever was typed in the zenity pop up entry dialog.

如果您现在查看my_variable中的值,它将是在 zenity 弹出条目对话框中键入的任何内容。

If you want to give some sort of prompt as to what the user (or you) should enter in the dialog, add the --textswitch with the label that you want. It looks something like this:

如果您想就用户(或您)应该在对话框中输入的内容给出某种提示,请添加带有所需标签的--text开关。它看起来像这样:

my_variable=$(zenity --entry --text="What's my variable:")

Zenity has lot of other nice options that are for specific tasks, so you might want to check those out as well with zenity --help. One example is the --calendaroption that let's you select a date from a graphical calendar.

Zenity 有许多其他不错的选项可用于特定任务,因此您可能还想使用zenity --help检查这些选项。一个例子是--calendar选项,让您从图形日历中选择一个日期。

my_date=$(zenity --calendar)

Which gives a nicely formatted date based on what the user clicked on:

它根据用户单击的内容提供了格式良好的日期:

echo ${my_date}

gives:

给出:

08/05/2009

08/05/2009

There are also options for slider selectors, errors, lists and so on.

还有滑块选择器、错误、列表等选项。

Hope this helps.

希望这可以帮助。

回答by chmac

If you are using Ubuntumany distros the notify-sendcommand will throw one of those nice perishable notifications in the top right corner. Like so:

如果您使用Ubuntu很多发行版,该notify-send命令将在右上角抛出那些不错的易腐通知之一。像这样:

notify-send "My name is bash and I rock da house"

notify-send "My name is bash and I rock da house"

B.e.a.utiful!

美丽的!

回答by Stéphane Gourichon

Everyone mentions zenity, there seem to be many others. A mixed up but interesting list is at http://alternativeto.net/software/zenity/

每个人都提到zenity,似乎还有很多其他的。一个混合但有趣的列表位于http://alternativeto.net/software/zenity/

First, an example of zenity featuring text formatting markup, window title, button label.

首先,以文本格式标记、窗口标题、按钮标签为特色的 zenity 示例。

zenity \
--info \
--text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nGet your <b>coffee</b>." \
--title="Coffee time" \
--ok-label="Sip"

gxmessage

消息

gxmessage "my text"

xmessage

消息

xmessageis very old so it is stable and probably available in all distributions that use X (since it's distributed with X). It is customizable through X resources, for those that have been using Linux or Unix for long enough to know what it means (.Xdefaults, anyone ?).

xmessage非常古老,因此它很稳定,并且可能在所有使用 X 的发行版中都可用(因为它与 X 一起发行)。它可以通过 X 资源进行定制,对于那些已经使用 Linux 或 Unix 足够长的时间并知道它意味着什么的人来说(.Xdefaults任何人?)。

xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse "Is xmessage enough for the job ?" -timeout 10

kdialog

k对话框

(not tested)

(未测试)

In a PPA

在 PPA 中

YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog. Does not seem to auto-size dialogs.

YAD:Zenity On Steroids [显示来自 Shell 脚本的图形对话框] ~ Web Upd8:Ubuntu/Linux 博客。似乎没有自动调整对话框大小。

echo My text | yad \
--text-info \
--width=400 \
--height=200

An bigger example

一个更大的例子

yad \
--title="Desktop entry editor" \
--text="Simple desktop entry editor" \
--form \
--field="Type:CB" \
--field="Name" \
--field="Generic name" \
--field="Comment" \
--field="Command:FL" \
--field="Icon" \
--field="In terminal:CHK" \
--field="Startup notify:CHK" "Application" "Name" "Generic name" "This is the comment" "/usr/bin/yad" "yad" FALSE TRUE \
--button="WebUpd8:2" \
--button="gtk-ok:0" \
--button="gtk-cancel:1"

Others not in Ubuntu standard repositories

其他不在 Ubuntu 标准存储库中的

  • shellgui
  • xdialog
  • gtkdialog
  • 贝壳类
  • 对话
  • gtk对话框

Off-topic (for terminal)

题外话(终端)

whiptail --msgbox "my text" 10 20
dialog --msgbox "my text" 10 20

Feel free to edit.

随意编辑。

回答by mulya

How about Ubuntu's alert. It can be used after any operation to alert it finished and even show red cross icon if operaton was finnished with errors

Ubuntu 的alert怎么样。可以在任何操作后使用它来提醒它完成,如果操作完成错误甚至显示红叉图标

ls -la; alert