从tk GUI运行shell脚本

时间:2017-11-05 16:13:46

标签: shell tcl tk

我有5个不同的shell脚本从不同的日志文件中提取数据,我在Tcl / Tk中编写了一个GUI,将所有脚本合并到一个接口,并且还创建了输入条目小部件,以便为使用的变量提供文件夹位置在存储日志的shell脚本中,我没有得到的一件事是如何将我的shell脚本与Tk GUI命令按钮集成并在文本小部件中显示输出。

任何建议。

谢谢

1 个答案:

答案 0 :(得分:1)

如果shell脚本运行起来相当快,最好的方法是使用exec

# If the scripts are marked executable
set output [exec thescript.sh $argumentOne $argumentTwo]
# If the scripts are not marked executable (assuming that they're really bash scripts)
set output [exec bash thescript.sh $argumentOne $argumentTwo]

您可以通过将entryttk::entry窗口小部件绑定到全局变量并使用它来设置参数,或者您可以使用任意数量的其他机制。 (有很多可能性。)输出放在output变量中;您可以通过将其插入text窗口小部件来显示它:

set outputwidget [text .t]
# Remember to put this widget in the right place in your GUI!
#    pack $outputwidget

# Put the output in the widget
$outputwidget insert end $output

# Stop the user from editing the contents.
# Note that you need to change the state to normal from code to update it from your script
$outputwidget configure -state readonly

你还可以做很多事情。 exec还有其他选项可以在某些情况下提供帮助,在向用户展示文字之前处理文字的整个可能性,以及标记文字可以做很多事情在小部件中执行诸如语法突出显示等操作。许多这些事情都很复杂,您需要将它们作为单独的问题。

此外,如果您有长时间运行的脚本,那么您将使用管道(使用open |…创建)或(旧的但非常好的)Expect包。这些也是很重要的话题。

相关问题