mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
d2a9e77748
when exit from a recursive called eventloop * ext/tk/lib/tk/timer.rb: new TkRTTimer class, which can works for a realtime operation * ext/tk/sample/tkrttimer.rb: sample of TkRTTimer class * ext/tk/lib/tk/textmark.rb: move TkTextMark#+ and TkTextMark#- to TkText::IndexModMethods * ext/tk/lib/tk/text.rb: improve TkTextMark#+ and TkTextMark#-, and add them to TkText::IndexModMethods module * ext/tk/sample/tktextio.rb: add test part of "seek by text index modifiers" git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
68 lines
2 KiB
Ruby
68 lines
2 KiB
Ruby
#!/usr/bin/env ruby
|
|
# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
|
|
|
|
require "tk"
|
|
|
|
root = TkRoot.new(:title=>'realtime timer sample')
|
|
|
|
f1 = TkFrame.new(:borderwidth=>2, :relief=>:ridge)
|
|
f1.pack(:side=>:bottom, :fill=>:both)
|
|
TkLabel.new(f1, :text=>'use TkTimer (TkAfter) class').pack(:anchor=>:center)
|
|
label1 = TkLabel.new(:parent=>f1, :relief=>:raised,
|
|
:width=>10).pack(:fill=>:both)
|
|
|
|
f2 = TkFrame.new(:borderwidth=>2, :relief=>:ridge)
|
|
f2.pack(:side=>:bottom, :fill=>:both)
|
|
TkLabel.new(f2, :text=>'use TkRTTimer class').pack
|
|
label2 = TkLabel.new(:parent=>f2, :relief=>:raised,
|
|
:width=>10).pack(:fill=>:both)
|
|
|
|
TkLabel.new(:text=>'Interval setting of each timer is 10 ms.',
|
|
:padx=>10, :pady=>5).pack
|
|
|
|
# define the procedure repeated by the TkTimer object
|
|
tick = proc{|aobj| #<== TkTimer object
|
|
cnt = aobj.return_value + 1 # return_value keeps a result of the last proc
|
|
label = aobj.current_args[0]
|
|
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)
|
|
}
|
|
|
|
timer1 = TkTimer.new(10, -1, [tick, label1]) # 10 ms interval
|
|
timer2 = TkRTTimer.new(10, -1, [tick, label2]) # 10 ms interval
|
|
|
|
timer1.start(0, proc{ label1.text('0.00'); 0 })
|
|
timer2.start(0, proc{ label2.text('0.00'); 0 })
|
|
|
|
b_start = TkButton.new(:text=>'Start', :state=>:disabled) {
|
|
pack(:side=>:left, :fill=>:both, :expand=>true)
|
|
}
|
|
|
|
b_stop = TkButton.new(:text=>'Stop', :state=>:normal) {
|
|
pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
|
|
}
|
|
|
|
b_start.command {
|
|
timer1.continue
|
|
timer2.continue
|
|
b_stop.state(:normal)
|
|
b_start.state(:disabled)
|
|
}
|
|
|
|
b_stop.command {
|
|
timer1.stop
|
|
timer2.stop
|
|
b_start.state(:normal)
|
|
b_stop.state(:disabled)
|
|
}
|
|
|
|
TkButton.new(:text=>'Reset', :state=>:normal) {
|
|
command { timer1.reset; timer2.reset }
|
|
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
|