Skip to content

Instantly share code, notes, and snippets.

@adamnew123456
Created February 15, 2020 05:39
Show Gist options
  • Save adamnew123456/008a25ec649bd7bf98e6824040d8a4d3 to your computer and use it in GitHub Desktop.
Save adamnew123456/008a25ec649bd7bf98e6824040d8a4d3 to your computer and use it in GitHub Desktop.
Day Clock
package require Tk
wm title . "Dayclock"
wm geometry . 200x225
canvas .clock
pack .clock
set expired_slice [.clock create arc 1 1 200 200 \
-start 225 -extent -270 -fill red]
set remaining_slice [.clock create arc 1 1 200 200 \
-start 225 -extent 0 -fill green]
set label [.clock create text 100 200 -text ""]
.clock create arc 1 1 200 200 -start 315 -extent 270
set start_hour 9
set end_hour 18
set duration [expr {$end_hour - $start_hour}]
proc unpad {value} {
if {$value == 0} {
return 0
} else {
return [regsub {^0+} $value {}]
}
}
proc round2 {value} {
return [expr {round($value * 100.0) / 100.0}]
}
proc draw_slices {} {
global start_hour end_hour duration label expired_slice remaining_slice
set hour [unpad [clock format [clock seconds] -format %H]]
set minute [unpad [clock format [clock seconds] -format %M]]
set total_range [expr {60.0 * $duration}]
set frac_hour [expr {($hour - $start_hour) + $minute/60.0}]
if {$frac_hour >= $start_hour && $frac_hour <= $end_hour} {
set expired_start 225
set expired_angle [expr {($frac_hour / $duration) * 270}]
set remaining_start [expr {$expired_start - $expired_angle}]
set remaining_angle [expr {270 - $expired_angle}]
set hours_left [expr {$duration - $frac_hour}]
.clock itemconfigure $label -text "[round2 $hours_left] remaining"
} else {
set expired_start 225
set expired_angle 270
set remaining_start 225
set remaining_angle 0
.clock itemconfigure $label -text "No time remaining"
}
.clock itemconfigure $expired_slice -start $expired_start -extent -$expired_angle
.clock itemconfigure $remaining_slice -start $remaining_start -extent -$remaining_angle
after 15000 draw_slices
}
draw_slices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment