Bash Dialog为按钮添加按钮和功能?

时间:2018-01-19 18:18:30

标签: bash dialog

我本质上是尝试从python迁移到bash以减少开销,但仍提供一些GUI功能。

我有这个代码并找到了添加按钮的方法但是我需要添加另一个按钮并找出如何将功能连接到按钮?一个按钮会调用另一个程序来运行,另一个按钮会停止/杀死其他程序。

有人可以告诉我一个如何将功能连接到按钮的示例吗?

    #!/bin/bash
    # useradd1.sh - A simple shell script to display the form dialog on screen
    # set field names i.e. shell variables
    call1="w5dmh"
    call2="kd8pgb"
    message="here is a message"

    # open fd
    exec 3>&1

    # Store data to $VALUES variable
    VALUES=$(dialog --extra-button --extra-label "Stop Beacon" --ok-label "Start Beacon" \
      --backtitle "PSKBeacon Setup" \
      --title "PSKBeacon" \
      --form "Create a new beacon" \15 50 0 \
    "From Callsign:" 1 1    "$call2"        1 18 8 0 \
    "To Callsign:"    2 1   "$call1"        2 18 8 0 \
    "Message:"    3 1       "$message"      3 18 25 0 \
     2>&1 1>&3)

            # close fd
            exec 3>&-

    beacon="$call1 $call1 de $call2 $call2 $message"
    [ -e pskbeacon.txt ] && rm pskbeacon.txt
    # display values just entered
    echo $beacon >>pskbeacon.txt

1 个答案:

答案 0 :(得分:0)

dialog程序的退出状态告诉您选择了什么“按钮”,dialog调用的输出(保存到VALUES变量)包含表单字段的内容,假设选择了“ok”或“extra”之一。 (你分别标记这些“开始”和“停止”。)

因此,您需要添加一些代码来检查dialog的退出状态,并从VALUES中提取值。类似下面的内容,在dialog调用后收听故事:

# Just after the "Store data to $VALUES variable"
# VALUES=$(dialog ... )

ret=$?

# close fd
exec 3>&-

case "$ret" in
  0) choice='START' ;; # the 'ok' button
  1) echo 'Cancel chosen'; exit ;;
  3) choice='STOP' ;; # the 'extra' button
  *) echo 'unexpected (ESC?)'; exit ;;
esac

# No exit, so start or stop chosen, and dialog should have
# emitted values (updated, perhaps), stored in VALUES now

{
  read -r call1
  read -r call2
  read -r message
} <<< "$VALUES"

# setting beacon differently: include choice, and quote form values
beacon="$choice: '$call1' de '$call2' '$message'"

[ -e pskbeacon.txt ] && rm pskbeacon.txt
# display values just entered
echo $beacon >>pskbeacon.txt

正如您所说,将函数“连接”到按钮可以像在我的示例中测试变量choice一样简单:

if [[ "$choice" = 'START' ]]; then
  echo 'Do something real here for START'
elif [[ "$choice" = 'STOP' ]]; then
  echo 'Do something real here for STOP'
fi