tags:

views:

335

answers:

2

I borrowed some code from a site, but I don't know how to get it to display.

class Stopwatch
  def start
    @accumulated = 0 unless @accumulated
    @elapsed = 0
    @start = Time.now
    @mybutton.configure('text' => 'Stop')
    @mybutton.command { stop }
    @timer.start
  end

  def stop
    @mybutton.configure('text' => 'Start')
    @mybutton.command { start }
    @timer.stop
    @accumulated += @elapsed
  end

  def reset
    stop
    @accumulated, @elapsed = 0, 0
    @mylabel.configure('text' => '00:00:00.00.000')
  end

  def tick
    @elapsed = Time.now - @start
    time = @accumulated + @elapsed
    h = sprintf('%02i', (time.to_i / 3600))
    m = sprintf('%02i', ((time.to_i % 3600) / 60))
    s = sprintf('%02i', (time.to_i % 60))
    mt = sprintf('%02i', ((time - time.to_i)*100).to_i)
    ms = sprintf('%04i', ((time - time.to_i)*10000).to_i)
    ms[0..0]=''
    newtime = "#{h}:#{m}:#{s}.#{mt}.#{ms}"
    @mylabel.configure('text' => newtime)
  end
end

How would I go about getting this running? Thanks

+2  A: 

I believe you have found the example on this site

I'm repeating what is already on the site but you are missing:

require 'tk'

as well as initialization code:

 def initialize
        root =  TkRoot.new { title 'Tk Stopwatch' }

        menu_spec = [
                     [
                      ['Program'],
                      ['Start', lambda { start } ],
                      ['Stop', lambda { stop } ],
                      ['Exit', lambda { exit } ]
                     ],
                     [
                      ['Reset'], ['Reset Stopwatch', lambda { reset } ]
                     ]
                    ]

        @menubar = TkMenubar.new(root, menu_spec, 'tearoff' => false)
        @menubar.pack('fill'=>'x', 'side'=>'top')

        @myfont = TkFont.new('size' => 16, 'weight' => 'bold')

        @mylabel = TkLabel.new(root)
        @mylabel.configure('text' => '00:00:00.0', 'font' => @myfont)
        @mylabel.pack('padx' => 10, 'pady' => 10)
        @mybutton =  TkButton.new(root)
        @mybutton.configure('text' => 'Start')
        @mybutton.command { start }
        @mybutton.pack('side'=>'left', 'fill' => 'both')


        @timer = TkAfter.new(1, -1, proc { tick })

        Tk.mainloop
      end
    end

    Stopwatch.new

I would suggest reading through the rest of the site to understand what is all going on.

Ryan Neufeld
Yeah i know that. I want to use Shoes instead and was trying to convert it as such. I would preferably just like it to return something, even just in the terminal.
danhere
+1  A: 

Based upon the additional code rkneufeld posted, this class requires a timer that is specific to Tk. To do it on the console, you could just create a loop that calls tick over and over. Of course, you have to remove all the code that was related to the GUI:

class Stopwatch
  def start
    @accumulated = 0 unless @accumulated
    @elapsed = 0
    @start = Time.now
#    @mybutton.configure('text' => 'Stop')
#    @mybutton.command { stop }
#    @timer.start
  end

  def stop
#    @mybutton.configure('text' => 'Start')
#    @mybutton.command { start }
#    @timer.stop
    @accumulated += @elapsed
  end

  def reset
    stop
    @accumulated, @elapsed = 0, 0
#    @mylabel.configure('text' => '00:00:00.00.000')
  end

  def tick
    @elapsed = Time.now - @start
    time = @accumulated + @elapsed
    h = sprintf('%02i', (time.to_i / 3600))
    m = sprintf('%02i', ((time.to_i % 3600) / 60))
    s = sprintf('%02i', (time.to_i % 60))
    mt = sprintf('%02i', ((time - time.to_i)*100).to_i)
    ms = sprintf('%04i', ((time - time.to_i)*10000).to_i)
    ms[0..0]=''
    newtime = "#{h}:#{m}:#{s}.#{mt}.#{ms}"
#    @mylabel.configure('text' => newtime)
  end
end

watch = Stopwatch.new
watch.start
1000000.times do
  puts watch.tick
end

You'll end up with output like this:

00:00:00.00.000
00:00:00.00.000
00:00:00.00.000
...
00:00:00.00.000
00:00:00.00.000
00:00:00.01.160
00:00:00.01.160
...

Not particularly useful, but there it is. Now, if you're looking to do something similar in Shoes, try this tutorial that is very similar.

Pesto