如何使的bash脚本的GUI? [英] How to make a GUI for bash scripts?

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

问题描述

我要做出一些图形的对话框来我的脚本,但不知道怎么办。
我听到一些关于GTK服务器或类似的东西。
如果有人知道如何猛砸与TCL链接/ tk的我也满足了。

请不要张贴类似更改为C ++,因为我的项目
必须在Bash中的脚本;有没有其他的选择。

任何想法?

编辑:
感谢您的答案,但我不希望我可以将图形作为在控制台的颜色,但图形窗口,最小化等。
我将测试 xmessage ,但我不认为这将是我所寻找的。

编辑2:
我不想做一个简单的对话框就像是/否,但是像进度条和按钮,有点像游戏中的一些接口。


解决方案

实际使用GUI对话,可以考虑使用控制台提示之前。很多时候,您可以用简单的逃脱Y / N?提示,在bash您通过 阅读实现它命令 ..

 阅读-p做什么?
如果[$ REPLY = =Y];然后
    回声耶;
科幻

如果控制台提示符只是不会削减它,的zenity 是真正的使用方便,例如:

 的zenity --error --text =测试...
      的zenity --question --text =是否继续?

这只能在Linux / Gnome的(或者更确切地说,它只会被默认在这样的系统安装)。在方法将在pretty工作得任何平台(包括无头机,或通过SSH)

如果你需要什么比阅读或的zenity提供更复杂的更改为C ++真的是最好的方法(虽然我建议你对C的Python / Ruby的++这样的shell脚本置换任务)


  

我想对于一些奇怪的游戏,做简单的界面,为了健康什么的进度条是我想要的例子。变量健康是34,所以要进度栏填写百分之三十四


作为一个命令行脚本,它会使用Python:

  $出口卫生= 34
$蟒蛇-c进口操作系统,打印'*'* INT(os.environ.get('健康',0))
**********************************

或者正常化1和78之间的值(这样你就不会得到一个标准的终端尺寸换行):

  $蟒蛇-c进口操作系统,打印'*'* INT((INT(os.environ.get('健康',0))/ 100.0)* 78)

的zenity也有进度对话框

 #!/ bin / sh的

回声10;睡眠1
回声#更新邮件日志;睡眠1
回声20;睡眠1
回声#重置cron作业;睡眠1
回声50;睡眠1
回声此行只是被忽略;睡眠1
回声75;睡眠1
回声#重新引导系统;睡眠1
回声100;睡眠1
)|
的zenity --progress \\
  --title =更新系统日志\\
  --text =扫描邮件日志...\\
  --percentage = 0如果[$? = -1];然后
        的zenity --error \\
          --text =更新取消了。
科幻

正如我以前说过,如果的zenity不能做你需要什么,看看写你的游戏的事作为一个正确的脚本的Python / Ruby的/的Perl / C ++ /等,因为它听起来像你推的边界什么是shell脚本可以做的。

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.

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

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.

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.

解决方案

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

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?"

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)

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)

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

As a command-line script, it'd use 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):

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

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

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天全站免登陆