如何为 bash 脚本制作 GUI? [英] How to make a GUI for bash scripts?

查看:47
本文介绍了如何为 bash 脚本制作 GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的脚本制作一些图形对话框,但不知道如何制作.我听说了一些关于 GTK-Server 或类似的东西.如果有人知道如何将 Bash 与 tcl/tk 联系起来,我也很满意.

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.

请不要发布诸如更改为 C++"之类的内容,因为我的项目必须是 Bash 中的脚本;没有其他选择.

Please do not post something like "change to C++" because my project must be a script in Bash; there are no other options.

有什么想法吗?

感谢您的回答,但我不想要控制台中颜色的图形",而是我可以移动、最小化等的图形窗口.我将测试 xmessage,但我认为这不是我要搜索的内容.

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.

编辑 2:我不想做一个简单的对话框,比如是/否,而是一些界面,比如进度条和按钮,比如游戏.

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.

推荐答案

在实际使用 GUI 对话之前,请考虑使用控制台提示.很多时候你可以通过简单的y/n?"逃脱.提示,在 bash 中您可以通过 read 命令..

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..

read -p "Do something? ";
if [ $REPLY == "y" ]; then
    echo yay;
fi

如果控制台提示无法解决问题,Zenity 真的是 易于使用,例如:

If console prompt's just won't cut it, Zenity is really easy to use, for example:

      zenity --error --text="Testing..."
      zenity --question --text="Continue?"

这仅适用于 Linux/Gnome(或者更确切地说,它只会默认安装在此类系统上).read 方法几乎适用于任何平台(包括无头机器,或通过 SSH)

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)

如果您需要比 read 或 Zenity 提供的更复杂的东西,更改为 C++"确实是最好的方法(尽管对于此类 shell 脚本替换任务,我建议使用 Python/Ruby 而不是 C++)

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)

我想为一些奇怪的游戏做一个简单的界面,健康进度条什么的就是我想要的例子.变量HEALTH"是34,所以让进度条填满34/100

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

作为命令行脚本,它将使用 Python:

As a command-line script, it'd use Python:

$ export HEALTH=34
$ python -c "import os; print '*' * int(os.environ.get('HEALTH', 0))"
**********************************

或者将 1 到 78 之间的值标准化(这样您就不会在标准终端大小上换行):

Or to normalise the values between 1 and 78 (so you don't get line-wrapping on a standard terminal size):

$ python -c "import os; print '*' * int((int(os.environ.get('HEALTH', 0)) / 100.0) * 78)"

Zenity 还有一个进度对话框

Zenity also has a 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

正如我之前所说,如果 Zenity 不能满足您的需求,请考虑在 Python/Ruby/Perl/C++/etc 中将您的游戏内容编写为正确的"脚本,因为听起来您正在推动shell 脚本可以做什么..

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..

这篇关于如何为 bash 脚本制作 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆