运行 shell 来自 tk GUI 的脚本

run shell script from tk GUI

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

任何建议。

谢谢

如果 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 还有其他选项可以在某些情况下提供帮助,在将文本显示给用户之前完全有可能对文本进行处理,并且您可以通过在小部件中标记文本来执行许多操作,例如语法突出显示等。其中许多内容非常复杂,您需要将它们作为单独的问题来回答。

此外,如果您有长 运行ning 脚本,那么您将使用管道(使用 open |… 创建)或(旧但优秀的)Expect 包。这些本身也是大话题。