gpt4 book ai didi

ruby - 如何让秒表程序运行?

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:18 25 4
gpt4 key购买 nike

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

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

我将如何让它运行起来?谢谢

最佳答案

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

不是特别有用,但确实有用。现在,如果您想在 Shoes 中做类似的事情,请尝试 this tutorial这非常相似。

关于ruby - 如何让秒表程序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/858970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com