~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/tktimer3.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class.
 
3
 
 
4
require "tk"
 
5
 
 
6
# new notation :
 
7
#   * symbols are acceptable as keys or values of the option hash
 
8
#   * the parent widget can be given by :parent key on the option hash 
 
9
root = TkRoot.new(:title=>'timer sample')
 
10
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \
 
11
               .pack(:side=>:bottom, :fill=>:both)
 
12
 
 
13
# define the procedure repeated by the TkTimer object
 
14
tick = proc{|aobj| #<== TkTimer object
 
15
  cnt = aobj.return_value + 5 # return_value keeps a result of the last proc
 
16
  label.text format("%d.%02d", *(cnt.divmod(100)))
 
17
  cnt #==> return value is kept by TkTimer object
 
18
      #    (so, can be send to the next repeat-proc)
 
19
}
 
20
 
 
21
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
 
22
        # ==> repeat-interval : (about) 50 ms,  
 
23
        #     repeat : infinite (-1) times, 
 
24
        #     repeat-procedure : tick (only one, in this case)
 
25
        #
 
26
        # ==> wait-before-call-init-proc : 0 ms, 
 
27
        #     init_proc : proc{ label.text('0.00'); 0 }
 
28
        #
 
29
        # (0ms)-> init_proc ->(50ms)-> tick ->(50ms)-> tick ->....
 
30
 
 
31
b_start = TkButton.new(:text=>'Start', :state=>:disabled) {
 
32
  pack(:side=>:left, :fill=>:both, :expand=>true)
 
33
}
 
34
 
 
35
b_stop  = TkButton.new(:text=>'Stop', :state=>:normal) {
 
36
  pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
 
37
}
 
38
 
 
39
b_start.command {
 
40
  timer.continue
 
41
  b_stop.state(:normal)
 
42
  b_start.state(:disabled)
 
43
}
 
44
 
 
45
b_stop.command {
 
46
  timer.stop
 
47
  b_start.state(:normal)
 
48
  b_stop.state(:disabled)
 
49
}
 
50
 
 
51
TkButton.new(:text=>'Reset', :state=>:normal) {
 
52
  command { timer.reset }
 
53
  pack(:side=>:right, :fill=>:both, :expand=>:yes)
 
54
}
 
55
 
 
56
ev_quit = TkVirtualEvent.new('Control-c', 'Control-q')
 
57
Tk.root.bind(ev_quit, proc{Tk.exit}).focus
 
58
 
 
59
Tk.mainloop