-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_gui.rb
57 lines (50 loc) · 1.18 KB
/
clock_gui.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require "observer"
require "tk"
class WatchModel
include Observable
def initialize
@running = false
@time = 0
@last = 0.0
Thread.start do
loop do
sleep 0.01
if @running
now = Time.now.to_f
@time += now - @last
@last = now
changed
notify_observers(@time)
end
end
end
end
def start_stop
@last = Time.now.to_f
@running = ! @running
end
def time
@time
end
end
class WatchWindow
def initialize
model = WatchModel.new
model.add_observer(self)
@label = TkLabel.new(nil).pack('fill'=>'x')
self.update(0)
btn = TkButton.new
btn.text('start/stop')
btn.command(proc{model.start_stop})
btn.pack('fill'=>'x')
btn = TkButton.new
btn.text('quit')
btn.command(proc{exit})
btn.pack('fill'=>'x')
Tk.mainloop
end
def update(time)
@label.text format("%02d:%02d", time.to_i, (time - time.to_i)*100)
end
end
WatchWindow.new