2003-06-19 12:14:43 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
|
|
|
|
|
|
|
|
require "tk"
|
|
|
|
|
2003-06-21 08:55:17 -04:00
|
|
|
# new notation :
|
|
|
|
# * symbols are acceptable as keys or values of the option hash
|
2009-03-05 22:56:38 -05:00
|
|
|
# * the parent widget can be given by :parent key on the option hash
|
2003-06-21 08:55:17 -04:00
|
|
|
root = TkRoot.new(:title=>'timer sample')
|
|
|
|
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \
|
2003-06-19 12:14:43 -04:00
|
|
|
.pack(:side=>:bottom, :fill=>:both)
|
|
|
|
|
2003-06-21 08:55:17 -04:00
|
|
|
# define the procedure repeated by the TkTimer object
|
|
|
|
tick = proc{|aobj| #<== TkTimer object
|
|
|
|
cnt = aobj.return_value + 5 # return_value keeps a result of the last proc
|
2003-06-19 12:14:43 -04:00
|
|
|
label.text format("%d.%02d", *(cnt.divmod(100)))
|
2003-06-21 08:55:17 -04:00
|
|
|
cnt #==> return value is kept by TkTimer object
|
|
|
|
# (so, can be send to the next repeat-proc)
|
2003-06-19 12:14:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
|
2009-03-05 22:56:38 -05:00
|
|
|
# ==> repeat-interval : (about) 50 ms,
|
|
|
|
# repeat : infinite (-1) times,
|
2003-06-21 08:55:17 -04:00
|
|
|
# repeat-procedure : tick (only one, in this case)
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# ==> wait-before-call-init-proc : 0 ms,
|
2003-06-21 08:55:17 -04:00
|
|
|
# init_proc : proc{ label.text('0.00'); 0 }
|
|
|
|
#
|
|
|
|
# (0ms)-> init_proc ->(50ms)-> tick ->(50ms)-> tick ->....
|
2003-06-19 12:14:43 -04:00
|
|
|
|
|
|
|
TkButton.new(:text=>'Start') {
|
|
|
|
command proc{ timer.continue unless timer.running? }
|
|
|
|
pack(:side=>:left, :fill=>:both, :expand=>true)
|
|
|
|
}
|
2003-07-30 03:15:00 -04:00
|
|
|
TkButton.new(:text=>'Restart') {
|
|
|
|
command proc{ timer.restart(0, proc{ label.text('0.00'); 0 }) }
|
|
|
|
pack(:side=>:left, :fill=>:both, :expand=>true)
|
|
|
|
}
|
2003-06-19 12:14:43 -04:00
|
|
|
TkButton.new(:text=>'Stop') {
|
|
|
|
command proc{ timer.stop if timer.running? }
|
|
|
|
pack('side'=>'right','fill'=>'both','expand'=>'yes')
|
|
|
|
}
|
|
|
|
|
|
|
|
ev_quit = TkVirtualEvent.new('Control-c', 'Control-q')
|
|
|
|
Tk.root.bind(ev_quit, proc{Tk.exit}).focus
|
|
|
|
|
|
|
|
Tk.mainloop
|