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
I was searching for a quick and dirty stop watch class to avoid coding such and came upon the site where the original code was posted and this site as well.
In the end, I modified the code until it met what I think that I was originally searching for.
In case anyone is interested, the version that I have ended up thus far with is as follows (albeit that I have yet to apply it in the application that I am currently updating and for which I want to make use of such functionality).
Simple stopwatch script:
Run like this in your terminal (to mark a lap, just tap enter):
I believe you have found the example on this site
I'm repeating what is already on the site but you are missing:
as well as initialization code:
I would suggest reading through the rest of the site to understand what is all going on.
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:
You'll end up with output like this:
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.