From: amnesiacx Date: 2004-01-09T22:16:39+09:00 Subject: Re: Call method from a TK object On Fri, 9 Jan 2004 21:27:22 +0900, ts wrote: >>>>>> "a" == amnesiacx writes: > >a> Nope, calling StopWatch.tick or StopWatch.zero doesnt work, "no such >a> method" (sic) error > > You make the confusion between StopWatch::tich (class method) and > StopWatch#tick (instance method). Try this (see 'self_orig = self') > >svg% cat b.rb >#!/usr/bin/ruby >class StopWatch > > @@seconds=0 > @@hundredths=0 > @@stopped=TRUE > $heading = "Ticker" > > def initialize(head) > $heading = head > gui $heading > root = Tk.root > root.bind "Control-c", proc{root.destroy} > root.bind "Control-q", proc{root.destroy} > Tk.root.focus > Tk.mainloop > end > > private > def gui(head) > > require "tk" > self_orig = self > > $title = TkLabel.new { > text head > relief 'raised' > width 20 > font 'courier' > pack('side'=>'top', 'fill'=>'both') > } > > $label = TkLabel.new { > text '0.00' > relief 'raised' > width 10 > font 'verdana' > pack('side'=>'bottom', 'fill'=>'both') > } > > $start = TkButton.new { > text 'Start' > command proc { > $stop.text "Stop" > if @@stopped > @@stopped = FALSE > self_orig.tick > end > } > pack('side'=>'left','fill'=>'both','expand'=>'yes') > } > > $stop = TkButton.new { > text 'Stop' > command proc{ > text "Zero" > if @@stopped then > self_orig.zero > text 'Stop' > end > @@stopped = TRUE > } > > pack('side'=>'right','fill'=>'both','expand'=>'yes') > } > end > > protected > def tick > if @@stopped then return end > Tk.after 50, proc{tick} > @@hundredths+=5 > if @@hundredths >= 100 > @@hundredths=0 > @@seconds+=1 > end > $label.text format("%d.%02d", @@seconds, @@hundredths) > end > > def zero > @@seconds=0 > @@hundredths=0 > $label.text format("%d.%02d", @@seconds, @@hundredths) > end > >end > >StopWatch.new("BIT Ruby Stop Watch!") > >svg% > > >p.s. : public is useless with initialize (initialize is always private) > > >Guy Decoux Thx for the help. Appreciated.