Tcl/Tk 动态时钟

一、电子时钟:

 


 proc every {ms body} {eval $body; after $ms [info level 0]}        

 pack [label .clock -textvar time]                                

 every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]}

 

 

 

 

二、模拟时钟

 

 

grid [canvas .c -width 200 -height 200]
set halfpi  1.570796
set piover6 0.5235987
set twopi   6.283185

.c create oval 2 2 198 198 -fill white -outline black
for { set h 1 } { $h <= 12 } { incr h } {
    set angle [expr { $halfpi - $piover6 * $h }]
    set x [expr { 100 + 90 * cos($angle) }]
    set y [expr { 100 - 90 * sin($angle) }]
    .c create text $x $y -text $h -font {Helvetica -12}
}

proc hands {} {
    catch { .c delete withtag hands }

    # Compute seconds since midnight

    set s [expr { [clock seconds] - [clock scan 00:00:00] }]

    # Angle of second hand

    set angle [expr { $s * $::twopi / 60. }]
    set y [expr { 100 - 90 * cos($angle) }]
    set x [expr { 100 + 90 * sin($angle) }]
    .c create line 100 100 $x $y -width 1 -tags hands

    # Minute hand

    set angle [expr { $s * $::twopi / 60. / 60. }]
    set y [expr { 100 - 85 * cos($angle) }]
    set x [expr { 100 + 85 * sin($angle) }]
    .c create line 100 100 $x $y -width 3 -capstyle projecting -tags  hands

    # Hour hand

    set angle [expr { $s * $::twopi / 60. / 60. / 12. }]
    set y [expr { 100 - 60 * cos($angle) }]
    set x [expr { 100 + 60 * sin($angle) }]
    .c create line 100 100 $x $y -width 7 -capstyle projecting -tags hands

    after 1000 hands

}
hands

 

 

 

 

 

三、二进制时钟

 

 

set radius 35

wm title . "BClock, initializing..."
wm maxsize . [expr $radius*6+1] [expr $radius*4+1]
wm minsize . [expr $radius*6+1] [expr $radius*4+1]
wm geometry . [expr $radius*6+1]x[expr $radius*4+1]

pack [canvas .b -background black]

foreach col {0 1 2 3 4 5} {
        foreach bit {0 1 2 3} {
                set x1 [expr $col * $radius]
                set y1 [expr $radius*3 - $bit * $radius]
                set x2 [expr $x1 + $radius]
                set y2 [expr $y1 + $radius]
                set layout(x${col}y${bit}) [.b create oval $x1 $y1 $x2 $y2]
        }
}

proc delay {ms body} {
        eval $body
        after $ms [list delay $ms $body]
}

delay 1000 {
        global layout
        set time [ clock format [ clock sec ] -format "%T" ]
        regexp {([0-2])([0-9]):([0-5])([0-9]):([0-5])([0-9])} /
                $time -> h1 h2 m1 m2 s1 s2

        wm title . "BClock, $time"
        set values [list $h1 $h2 $m1 $m2 $s1 $s2]
        foreach col {0 1 2 3 4 5} {
                set value [lindex $values $col]
                foreach bit {0 1 2 3} {
                        if { $value & (1 << $bit) } {
                                set colour IndianRed1
                        } else {
                                set colour DarkRed
                        }
                        .b itemconfigure $layout(x${col}y${bit}) -fill $colour
                }
        }
}

 

 

 

 

 

 

 关于时钟,还有其他版本的,如

 

 

 

 

 

 

 

 

 

 

参考 来源:http://wiki.tcl.tk/1011

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值