如何为 bash 脚本制作 GUI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/928019/
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
How to make a GUI for bash scripts?
提问by lauriys
I want to make some graphical dialogs for my script but don't know how. I hear something about GTK-Server or something like that. If someone knows how to link Bash with tcl/tk I also be satisfied.
我想为我的脚本制作一些图形对话框,但不知道如何制作。我听说了一些关于 GTK-Server 或类似的东西。如果有人知道如何将 Bash 与 tcl/tk 联系起来,我也很满意。
Please do not post something like "change to C++" because my project must be a script in Bash; there are no other options.
请不要发布诸如“更改为 C++”之类的内容,因为我的项目必须是 Bash 中的脚本;没有其他选择。
Any ideas?
有任何想法吗?
EDIT:Thanks for answers but I don't want "graphics" as in colors in the console, but graphical windows which I can move, minimize etc.
I will test xmessage
, but I don't think that will be what I am searching for.
编辑:感谢您的回答,但我不想要控制台中颜色的“图形”,而是我可以移动、最小化等的图形窗口。我会测试xmessage
,但我认为这不是我正在寻找的.
EDIT 2:I don't want make a simple dialog like yes/no, but some interface like progress bars and buttons, something like a game.
编辑 2:我不想做一个简单的对话框,比如是/否,而是一些界面,比如进度条和按钮,比如游戏。
采纳答案by dbr
Before actually using GUI dialogues, consider using console prompts. Quite often you can get away with simple "y/n?" prompts, which in bash you achieve via the read
command..
在实际使用 GUI 对话之前,请考虑使用控制台提示。很多时候你可以通过简单的“y/n?”逃脱。提示,在 bash 中您可以通过read
命令..
read -p "Do something? ";
if [ $REPLY == "y" ]; then
echo yay;
fi
If console prompt's just won't cut it, Zenityis really easy to use, for example:
如果控制台提示不会切断它,Zenity真的很容易使用,例如:
zenity --error --text="Testing..."
zenity --question --text="Continue?"
This only works on Linux/Gnome (or rather, it'll only be installed by default on such systems). The read
method will work on pretty much any platform (including headless machines, or via SSH)
这仅适用于 Linux/Gnome(或者更确切地说,它只会默认安装在此类系统上)。该read
方法几乎适用于任何平台(包括无头机器,或通过 SSH)
If you need anything more complex than what read or Zenity provides, "change to C++" is really the best method (although I'd recommend Python/Ruby over C++ for such shell-script-replacement tasks)
如果您需要比 read 或 Zenity 提供的更复杂的东西,“更改为 C++”确实是最好的方法(尽管对于此类 shell 脚本替换任务,我建议使用 Python/Ruby 而不是 C++)
I want to do simple interface for some strange game, the progress bar for health or something is the example for what I want. Variable "HEALTH" is 34, so make progress bar filled in 34/100
我想为一些奇怪的游戏做简单的界面,健康的进度条什么的就是我想要的例子。变量“健康”是 34,所以让进度条填满 34/100
As a command-line script, it'd use Python:
作为命令行脚本,它将使用 Python:
$ export HEALTH=34
$ python -c "import os; print '*' * int(os.environ.get('HEALTH', 0))"
**********************************
Or to normalise the values between 1 and 78 (so you don't get line-wrapping on a standard terminal size):
或者将 1 到 78 之间的值标准化(这样您就不会在标准终端大小上进行换行):
$ python -c "import os; print '*' * int((int(os.environ.get('HEALTH', 0)) / 100.0) * 78)"
Zenity also has a Progress Dialog,
Zenity 还有一个Progress Dialog,
#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "50" ; sleep 1
echo "This line will just be ignored" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
--title="Update System Logs" \
--text="Scanning mail logs..." \
--percentage=0
if [ "$?" = -1 ] ; then
zenity --error \
--text="Update canceled."
fi
As I said before, if Zenity cannot do what you need, look into writing your game-thing as a "proper" script in Python/Ruby/Perl/C++/etc as it sounds like you're pushing the bounds of what a shell-script can do..
正如我之前所说,如果 Zenity 无法满足您的需求,请考虑在 Python/Ruby/Perl/C++/etc 中将您的游戏内容编写为“正确的”脚本,因为这听起来像是您在推动 shell 的界限-脚本可以做..
回答by nxadm
If you want to write a graphical UI in bash, zenityis the way to go. This is what you can do with it:
如果您想用 bash 编写图形用户界面,zenity是您的最佳选择。这是你可以用它做的:
Application Options:
--calendar Display calendar dialog
--entry Display text entry dialog
--error Display error dialog
--info Display info dialog
--file-selection Display file selection dialog
--list Display list dialog
--notification Display notification
--progress Display progress indication dialog
--question Display question dialog
--warning Display warning dialog
--scale Display scale dialog
--text-info Display text information dialog
Combining these widgets you can create pretty usable GUIs. Of course, it's not as flexible as a toolkit integrated into a programming language, but in some cases it's really useful.
结合这些小部件,您可以创建非常有用的 GUI。当然,它不像集成到编程语言中的工具包那样灵活,但在某些情况下它确实很有用。
回答by Pierre
there is a command called dialog
which uses the ncurses library. "Dialog is a program that will let you to present a variety of questions or display messages using dialog boxes from a shell script. These types of dialog boxes are implemented (though not all are necessarily compiled into dialog)"
有一个dialog
使用 ncurses 库的命令。“ Dialog 是一个程序,它可以让您使用来自 shell 脚本的对话框来呈现各种问题或显示消息。实现了这些类型的对话框(尽管不一定都编译成对话框)”
回答by Charlie Martin
回答by Renato Aquino
回答by vaisarger
Please, take a look at my library: http://sites.google.com/site/easybashgui
请看看我的图书馆:http: //sites.google.com/site/easybashgui
It is intended to handle, with the same commands set, indifferently all four big tools "kdialog", "Xdialog", "cdialog" and "zenity", depending if X is running or not, if D.E. is KDE or Gnome or other. There are 15 different functions ( among them there are two called "progress" and "adjust" )...
它旨在使用相同的命令集处理所有四大工具“kdialog”、“Xdialog”、“cdialog”和“zenity”,这取决于 X 是否正在运行,DE 是 KDE 还是 Gnome 或其他。有 15 种不同的功能(其中有两个称为“进度”和“调整”)...
Bye :-)
再见 :-)
回答by dmitry_vk
You can gtk-serverfor this. Gtk-server is a program that runs in background and provides text-based interface to allow other programs (including bash scripts) to control it. It has examples for Bash (http://www.gtk-server.org/demo-ipc.bash.txt, http://www.gtk-server.org/demo-fifo.bash.txt)
您可以为此使用gtk-server。Gtk-server 是一个在后台运行的程序,它提供基于文本的界面以允许其他程序(包括 bash 脚本)对其进行控制。它有 Bash 的例子(http://www.gtk-server.org/demo-ipc.bash.txt,http://www.gtk-server.org/demo-fifo.bash.txt)
回答by Brian Carper
If you have Qt/KDE installed, you can use kdialog, which pops up a Qt dialog window. You can easily specify to display a Yes/No dialog, OK/Cancel, simple text input, password input etc. You then have access to the return values from these dialogs at the shell.
如果你安装了 Qt/KDE,你可以使用kdialog,它会弹出一个 Qt 对话框窗口。您可以轻松指定显示是/否对话框、确定/取消、简单文本输入、密码输入等。然后您可以在 shell 中访问这些对话框的返回值。
回答by Tamas Czinege
Apparently someone has written a bash interface for GTK+. I'm not sure if you can get it working easily, but it might worth a try. You can find it here:
显然有人为 GTK+ 编写了一个 bash 接口。我不确定你是否可以轻松地让它工作,但它可能值得一试。你可以在这里找到它: