1

我从一个网站借了一些代码,但我不知道如何让它显示。

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

我将如何让这个运行?谢谢

4

4 回答 4

4

根据 rkneufeld 发布的附加代码,这个类需要一个特定于 Tk 的计时器。要在控制台上执行此操作,您可以创建一个循环调用 tick 一遍又一遍。当然,您必须删除所有与 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

你最终会得到这样的输出:

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
...

不是特别有用,但确实有。现在,如果你想在鞋子中做类似的事情,试试这个非常相似的教程。

于 2009-05-13T17:59:25.953 回答
1

我相信你已经在这个网站上找到了这个例子

我在重复网站上已有的内容,但您缺少:

require 'tk'

以及初始化代码:

 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

我建议阅读网站的其余部分以了解发生了什么。

于 2009-05-13T16:50:48.373 回答
1

我正在寻找一个快速而肮脏的秒表类,以避免编写这样的代码,并找到了发布原始代码的网站和这个网站。

最后,我修改了代码,直到它符合我认为我最初搜索的内容。

如果有人感兴趣,到目前为止我最终使用的版本如下(尽管我还没有将它应用到我当前正在更新的应用程序中,并且我想使用这些功能)。

#    REFERENCES
#       1. http://stackoverflow.com/questions/858970/how-to-get-a-stopwatch-program-running
#       2. http://codeidol.com/other/rubyckbk/User-Interface/Creating-a-GUI-Application-with-Tk/
#       3. http://books.google.com.au/books?id=bJkznhZBG6gC&pg=PA806&lpg=PA806&dq=ruby+stopwatch+class&source=bl&ots=AlH2e7oWWJ&sig=KLFR-qvNfBfD8WMrUEbVqMbN_4o&hl=en&ei=WRjOTbbNNo2-uwOkiZGwCg&sa=X&oi=book_result&ct=result&resnum=8&ved=0CEsQ6AEwBw#v=onepage&q=ruby%20stopwatch%20class&f=false
#       4. http://4loc.wordpress.com/2008/09/24/formatting-dates-and-floats-in-ruby/


module Utilities
  class StopWatch
    def new()
      @watch_start_time = nil           #Time (in seconds) when the stop watch was started (i.e. the start() method was called).
      @lap_start_time   = nil           #Time (in seconds) when the current lap started.
    end  #def new


    def start()
      myCurrentTime = Time.now()        #Current time in (fractional) seconds since the Epoch (January 1, 1970 00:00 UTC)

      if (!running?) then    
        @watch_start_time = myCurrentTime         
        @lap_start_time   = @watch_start_time
      end  #if

      myCurrentTime - @watch_start_time;
    end  #def start


    def lap_time_seconds()
      myCurrentTime = Time.now()
      myLapTimeSeconds = myCurrentTime - @lap_start_time
      @lap_start_time  = myCurrentTime
      myLapTimeSeconds
    end  #def lap_time_seconds


    def stop()
      myTotalSecondsElapsed = Time.now() - @watch_start_time
      @watch_start_time = nil

      myTotalSecondsElapsed
    end  #def stop


    def running?()
      !@watch_start_time.nil?
    end  #def
  end  #class StopWatch
end  #module Utilities






def kill_time(aRepeatCount)
  aRepeatCount.times do
    #just killing time
  end  #do
end  #def kill_time



elapsed_time_format_string = '%.3f'

myStopWatch = Utilities::StopWatch.new()
puts 'total time elapsed: ' + elapsed_time_format_string % myStopWatch.start() + ' seconds'

kill_time(10000000)
puts 'lap time:           ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds'

kill_time(20000000)
puts 'lap time:           ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds'

kill_time(30000000)
puts 'lap time:           ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds'
puts 'total time elapsed: ' + elapsed_time_format_string % myStopWatch.stop() + ' seconds'
于 2011-05-15T09:40:31.770 回答
0

简单的秒表脚本:

# pass the number of seconds as the parameter

seconds = eval(ARGV[0]).to_i
start_time = Time.now

loop do
  elapsed = Time.now - start_time
  print "\e[D" * 17
  print "\033[K"

  if elapsed > seconds
    puts "Time's up!"
    exit
  end

  print Time.at(seconds - elapsed).utc.strftime('%H:%M:%S.%3N')
  sleep(0.05)
end

在终端中像这样运行(要标记一圈,只需点击 enter):

# 10 is the number of seconds
ruby script.rb 10 
# you can even do this:
ruby script.rb "20*60" # 20 minutes
于 2014-05-13T20:14:43.793 回答