summaryrefslogtreecommitdiff
path: root/ext/tk/sample
diff options
context:
space:
mode:
Diffstat (limited to 'ext/tk/sample')
-rw-r--r--ext/tk/sample/tktimer2.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/tk/sample/tktimer2.rb b/ext/tk/sample/tktimer2.rb
new file mode 100644
index 0000000000..0359ac4d08
--- /dev/null
+++ b/ext/tk/sample/tktimer2.rb
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
+
+require "tk"
+
+label = TkLabel.new(:relief=>:raised, :width=>10) \
+ .pack(:side=>:bottom, :fill=>:both)
+
+tick = proc{|aobj|
+ cnt = aobj.return_value + 5
+ label.text format("%d.%02d", *(cnt.divmod(100)))
+ cnt
+}
+
+timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
+
+TkButton.new(:text=>'Start') {
+ command proc{ timer.continue unless timer.running? }
+ 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