如何在linux中的bash脚本中显示GUI消息框?

时间:2008-08-10 04:07:55

标签: linux bash scripting ubuntu gtk

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

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

13 个答案:

答案 0 :(得分:138)

如果您使用 Ubuntu 许多发行版,notify-send命令将在右上角抛出其中一个漂亮的易腐通知。像这样:

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

B.e.a.utiful!

答案 1 :(得分:133)

我相信Zenity会做你想做的事。它专门用于从命令行显示GTK对话框,并以Ubuntu package形式提供。

答案 2 :(得分:71)

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

首先,以文本格式标记,窗口标题,按钮标签为特色的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

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

(未经测试)

在PPA中

YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog。似乎没有自动调整对话框大小。

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

更大的例子

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"

其他不在Ubuntu标准存储库中

  • shellgui
  • xdialog
  • gtkdialog

偏离主题(对于终端)

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

随意编辑。

答案 3 :(得分:46)

zenity 应用程序似乎是您正在寻找的。

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

my_variable=$(zenity --entry)

如果你现在查看 my_variable 中的值,它将是zenity弹出输入对话框中输入的内容。

如果您想提供某些用户(或您)应在对话框中输入的提示,请添加带有所需标签的 - 文本开关。它看起来像这样:

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

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

my_date=$(zenity --calendar)

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

echo ${my_date}

给出:

  

08/05/2009

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

希望这有帮助。

答案 4 :(得分:15)

我找到了xmessage命令,这很好。

答案 5 :(得分:7)

这是一个小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

这样称呼:

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

答案 6 :(得分:7)

alertnotify-send似乎是一回事。我使用notify-send作为非输入消息,因为它没有窃取焦点,我找不到阻止zenity等的方法。

e.g。

# This will display message and then disappear after a delay:
notify-send "job complete"

# This will display message and stay on-screen until clicked:
notify-send -u critical "job complete"

答案 7 :(得分:5)

还有dialog和KDE版本kdialog。 slackware使用dialog,因此可能无法立即在其他发行版上使用。

答案 8 :(得分:3)

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

ls -la; alert

答案 9 :(得分:3)

如果没有别的东西存在。你可以启动一个xterm并在其中回显,如下所示:

 xterm -e bash -c 'echo "this is the message";echo;echo -n "press enter to continue "; stty sane -echo;answer=$( while ! head -c 1;do true ;done);'

答案 10 :(得分:2)

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

zenity --help

答案 11 :(得分:0)

Kdialog和对话都很好,但我推荐Zenity。快速,简单,更好地查看xmessage或对话框。

答案 12 :(得分:0)

使用Gambas GTK / QT控件(GUI对象)的bash脚本示例: Gambas IDE甚至可以用于设计大型GUI并充当GUI服务器。 可以从Gambas App Store下载示例示例。
https://gambas.one/gambasfarm/?id=823&action=search

enter image description here