Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在Tcl/Tk语言中创建带有文本模式控件的计时器_Linux_Loops_Time_Tcl_Tk - Fatal编程技术网

Linux 如何在Tcl/Tk语言中创建带有文本模式控件的计时器

Linux 如何在Tcl/Tk语言中创建带有文本模式控件的计时器,linux,loops,time,tcl,tk,Linux,Loops,Time,Tcl,Tk,我想要的是与下面这个bash shell脚本类似的东西: 贝壳狂欢 也许最明显的是将其转换为Tcl/Tk。我甚至试过,但还是没有成功。见: 壳牌Tclsh 我仍然致力于使它与引用的示例-bash脚本一样工作。但不要排除你可以推广的改进和建议 我的想法是: 如果这里有人知道并想分享这个想法,请放心。您的Tcl代码有几个问题: proc_pause{}{-Tcl对空格非常敏感,因此需要将过程名与参数列表分开 s=[expr s+1]-使用set来设置变量,您需要使用$s来获取变量值:set s[ex

我想要的是与下面这个bash shell脚本类似的东西:

贝壳狂欢 也许最明显的是将其转换为Tcl/Tk。我甚至试过,但还是没有成功。见:

壳牌Tclsh 我仍然致力于使它与引用的示例-bash脚本一样工作。但不要排除你可以推广的改进和建议

我的想法是:


如果这里有人知道并想分享这个想法,请放心。

您的Tcl代码有几个问题:

proc_pause{}{-Tcl对空格非常敏感,因此需要将过程名与参数列表分开 s=[expr s+1]-使用set来设置变量,您需要使用$s来获取变量值:set s[expr{$s+1}],或者在本例中使用incr s命令 如果{$s-eq 60}和{$key=s},请参阅手册页以了解正确的运算符。 您需要{$s==60}和{$s} stty-echo-icanon min 0-stty是一个外部命令,因此您需要exec-stty。。。 这些是主要的语法问题。可以改进缩进样式,使代码可读性和可维护性

我认为这是一个有趣的挑战,所以我决定独立于您的代码来实现它。如果您有任何问题,请告诉我:

#!/usr/bin/env tclsh

set seconds 0
set running true
array set status {
    false "(paused)"
    true  "        "
}

#################################################################
proc main {} {
    enableRaw

    puts "'p' to pause; 'c' to continue; 'q' to quit"
    every 1000 display_time

    chan configure stdout -buffering none
    chan configure stdin -blocking no -buffering none
    chan event stdin readable handleStdin

    vwait ::forever

    disableRaw
    puts ""
}

# ref https://wiki.tcl.tk/14693
proc enableRaw {{channel stdin}} {
    exec /bin/stty raw -echo <@$channel
}
proc disableRaw {{channel stdin}} {
    exec /bin/stty -raw echo <@$channel
}

proc every {ms code} {
    after $ms [list every $ms $code]
    uplevel #0 $code
}

proc display_time {{event ""}} {
    global running seconds
    puts -nonewline "\r [format_time] $::status($running) "
    if {$running && $event eq ""} {incr seconds}
}

proc format_time {} {
    return [clock format $::seconds -format "%H:%M:%S" -gmt true]
}

proc handleStdin {} {
    set data [chan read stdin 1]
    switch -- $data {
        P - p {set ::running false; display_time}
        C - c {set ::running true;  display_time unpausing}
        Q - q {set ::forever "now"}
    }
}

#################################################################
main

您正在运行哪个版本的Tcl?输入[info patchlevel]-这显然是一个旧版本:为什么?解决方案:用fconfigure替换chan configure;用fileevent替换chan event;用read替换chan read我正在运行哪个版本的Tcl?Tcl/Tk 8.4。我怀疑您对某个版本的Tcl帐户的崩溃是相同的。这就是stty raw想要修复的,所以您不需要按enter键
#!/usr/bin/env tclsh
# shell timer
# Note: Do not measure time precisely because there is loss in calculations and other commands
# For a human being is something almost imperceptible, fortunately.
# ------------------------------------------------- -----------------------------
set s 00
set m 00
set h 00

puts -nonewline ""
flush stdout
set key [gets stdin]

proc _screen{ } {
clear


set archive [open [pwd]/time.txt w]

# Shows the elapsed time on the terminal screen and plays to the time.txt file always updating
puts $archive "%02d:%02d:%02d" $h $m $s" 
puts -nonewline ":: 'p' to pause, 'c' to continue and 's' to exit ::"


}

proc _time{ } {
    _screen
  after 1000
  s=[expr s+1]
  if { $s -eq 60 } { m=[expr m+1] } { s=00 }
  if { $m -eq 60 } { h=[expr h+1] } { m=00 }
}

proc _pause{ } {
while { 1 } 
{
    _screen
  after 1000
    $argv key
    if { "$key" = "c" } { break }
  }
}

proc _main{ } {

# Put the terminal in special character interpretation mode
stty -echo -icanon min 0

while { 1 } 
{
    if { "$key" = "s" } { break }
    if { "$key" = "p" } { _pause }
    _time
    $argv key

}

# Restores the default mode
stty sane
close $archive
exit 0
}
after 1000 _main
#!/usr/bin/env tclsh

set seconds 0
set running true
array set status {
    false "(paused)"
    true  "        "
}

#################################################################
proc main {} {
    enableRaw

    puts "'p' to pause; 'c' to continue; 'q' to quit"
    every 1000 display_time

    chan configure stdout -buffering none
    chan configure stdin -blocking no -buffering none
    chan event stdin readable handleStdin

    vwait ::forever

    disableRaw
    puts ""
}

# ref https://wiki.tcl.tk/14693
proc enableRaw {{channel stdin}} {
    exec /bin/stty raw -echo <@$channel
}
proc disableRaw {{channel stdin}} {
    exec /bin/stty -raw echo <@$channel
}

proc every {ms code} {
    after $ms [list every $ms $code]
    uplevel #0 $code
}

proc display_time {{event ""}} {
    global running seconds
    puts -nonewline "\r [format_time] $::status($running) "
    if {$running && $event eq ""} {incr seconds}
}

proc format_time {} {
    return [clock format $::seconds -format "%H:%M:%S" -gmt true]
}

proc handleStdin {} {
    set data [chan read stdin 1]
    switch -- $data {
        P - p {set ::running false; display_time}
        C - c {set ::running true;  display_time unpausing}
        Q - q {set ::forever "now"}
    }
}

#################################################################
main