diff options
author | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-31 20:52:40 +0000 |
---|---|---|
committer | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-31 20:52:40 +0000 |
commit | 75362fbd47cedf4b4906a361a6c54bc4ad8ea5ec (patch) | |
tree | 33e458bfb8dcf84face1eb34acede77b68ff5d8d /ext/tk/sample/demos-jp/timer | |
parent | 0cdf0d99c1e5164c53676a39265ff99120c8a026 (diff) | |
download | ruby-75362fbd47cedf4b4906a361a6c54bc4ad8ea5ec.tar.gz |
* (IMPORTANT BUG FIX) scan of event keywords doesn't work on recent
versions of Tck/Tk
* (bug fix) initialize error of instance variable on TkComposite
* (bug fix) initialize error on encoding-system on MultiTkIp
* (bug fix) trouble on destroying widgets
* (new) add JP and EN version of Ruby/Tk widget demos
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4249 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/tk/sample/demos-jp/timer')
-rw-r--r-- | ext/tk/sample/demos-jp/timer | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/ext/tk/sample/demos-jp/timer b/ext/tk/sample/demos-jp/timer new file mode 100644 index 0000000000..232f4ae5ca --- /dev/null +++ b/ext/tk/sample/demos-jp/timer @@ -0,0 +1,120 @@ +#!/usr/local/bin/ruby +# +# timer -- +# This script generates a counter with start,stop and reset buttons. +# +# Copyright (C) 1998 Takaaki Tateishi (ttate@jaist.ac.jp) +# last update: Sat Jun 27 12:24:14 JST 1998 +# + +require "tk" +require "thread" +require "tkafter" + +$time = "0.00" +$m = Mutex.new +$loop = false + +def timer_stop + $loop = false + $m.lock +end + +def timer_start + $loop = true + $m.unlock +end + +def timer_reset + $time = "0.00" + $root.countframe.counter['text'] = $time +end + +def timer_loop + if $loop + $time = $time.succ + $root.countframe.counter['text'] = $time + end + Tk.after(10,proc{timer_loop}) +end + + +# +# thread version +# +def timer_loop2 + while true + $m.lock + $time = $time.succ + $root.countframe.counter['text'] = $time + sleep(0.01) + $m.unlock + end +end + +# +# TkAfter +# +def timer_loop3 + if $loop + $time = $time.succ + $root.countframe.counter['text'] = $time + end +end + + +class CountFrame < TkFrame + attr_reader :counter + + def initialize(parent=nil,keys=nil) + super(parent,keys) + @counter = TkLabel.new(self, + 'text'=>$time, + 'relief'=>'raised') + @counter.pack('fill'=>'both') + self + end +end + + +class ButtonFrame < TkFrame + def initialize(parent=nil,keys=nil) + super(parent,keys) + @stop = TkButton.new(self, + 'text'=>'Stop', + 'command'=>proc{timer_stop}) + @start = TkButton.new(self, + 'text'=>'Start', + 'command'=>proc{timer_start}) + @reset = TkButton.new(self, + 'text'=>'Reset', + 'command'=>proc{timer_reset}) + for b in [@stop,@start,@reset] + b.pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes') + end + end +end + + +class Timer < TkRoot + attr_reader :countframe + + def initialize + super + @countframe = CountFrame.new(self) + @buttonframe = ButtonFrame.new(self) + for f in [@buttonframe,@countframe] + f.pack('side'=>'top', 'fill'=>'both') + end + self + end +end + + +$root = Timer.new + +#$thread = Thread.start{timer_loop2} +#timer_loop +TkAfter.new(10,-1,proc{timer_loop3}).start + +Tk.mainloop |