2
# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
7
# * symbols are acceptable as keys or values of the option hash
8
# * the parent widget can be given by :parent key on the option hash
9
root = TkRoot.new(:title=>'timer sample')
10
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \
11
.pack(:side=>:bottom, :fill=>:both)
13
# define the procedure repeated by the TkTimer object
14
tick = proc{|aobj| #<== TkTimer object
15
cnt = aobj.return_value + 5 # return_value keeps a result of the last proc
16
label.text format("%d.%02d", *(cnt.divmod(100)))
17
cnt #==> return value is kept by TkTimer object
18
# (so, can be send to the next repeat-proc)
21
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
22
# ==> repeat-interval : (about) 50 ms,
23
# repeat : infinite (-1) times,
24
# repeat-procedure : tick (only one, in this case)
26
# ==> wait-before-call-init-proc : 0 ms,
27
# init_proc : proc{ label.text('0.00'); 0 }
29
# (0ms)-> init_proc ->(50ms)-> tick ->(50ms)-> tick ->....
31
b_start = TkButton.new(:text=>'Start', :state=>:disabled) {
32
pack(:side=>:left, :fill=>:both, :expand=>true)
35
b_stop = TkButton.new(:text=>'Stop', :state=>:normal) {
36
pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
42
b_start.state(:disabled)
47
b_start.state(:normal)
48
b_stop.state(:disabled)
51
TkButton.new(:text=>'Reset', :state=>:normal) {
52
command { timer.reset }
53
pack(:side=>:right, :fill=>:both, :expand=>:yes)
56
ev_quit = TkVirtualEvent.new('Control-c', 'Control-q')
57
Tk.root.bind(ev_quit, proc{Tk.exit}).focus