1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/tk/sample/tktimer2.rb
naruse c4fdfabcc8 handle ext/ as r53141
g -L frozen_string_literal ext/**/*.rb|xargs ruby -Ka -e'ARGV.each{|fn|puts
fn;open(fn,"r+"){|f|s=f.read.sub(/\A(#!.*\n)?(#.*coding.*\n)?/,"\\&#
frozen_string_literal: false\n");f.rewind;f.write s}}'

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53143 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-16 05:31:54 +00:00

48 lines
1.7 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: false
# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
require "tk"
# new notation :
# * symbols are acceptable as keys or values of the option hash
# * the parent widget can be given by :parent key on the option hash
root = TkRoot.new(:title=>'timer sample')
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \
.pack(:side=>:bottom, :fill=>:both)
# 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
label.text format("%d.%02d", *(cnt.divmod(100)))
cnt #==> return value is kept by TkTimer object
# (so, can be send to the next repeat-proc)
}
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
# ==> repeat-interval : (about) 50 ms,
# repeat : infinite (-1) times,
# repeat-procedure : tick (only one, in this case)
#
# ==> wait-before-call-init-proc : 0 ms,
# init_proc : proc{ label.text('0.00'); 0 }
#
# (0ms)-> init_proc ->(50ms)-> tick ->(50ms)-> tick ->....
TkButton.new(:text=>'Start') {
command proc{ timer.continue unless timer.running? }
pack(:side=>:left, :fill=>:both, :expand=>true)
}
TkButton.new(:text=>'Restart') {
command proc{ timer.restart(0, proc{ label.text('0.00'); 0 }) }
pack(:side=>:left, :fill=>:both, :expand=>true)
}
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