def load
if File.exists?('status.txt')
File.open('status.txt', 'r') do |f|
$acctime, $running = Marshal.load(f.read)
end
if $running
$start.configure('text' => 'Stop')
$after.start
else
$start.configure('text' => 'Start')
end
update_timer
end
end
def save
%(
File.open('status.txt', 'w') do |f|
f.write Marshal.dump([$acctime, $running])
end)
end
def start
if $running
$acctime += Time.now - $running
$start.configure('text' => 'Start')
$running = nil
$after.stop
update_timer
else
$running = Time.now
$start.configure('text' => 'Stop')
$after.start
end
save
end
def update_timer
time = $acctime
if $running
time += Time.now - $running
end
h = time.to_i / 3600
m = (time.to_i % 3600) / 60
s = time.to_i % 60
micro = ((time - time.to_i)*10).to_i
time = sprintf("%02i:%02i:%02i.%1i", h, m, s, micro)
$timer.configure('text' => time)
end
最新回复
类 api里有介绍
api:http://api.rubyonrails.org/
推荐使用Ruby+GTK2开发Windows的应用程序!!
CODE:
#!/usr/local/bin/ruby
require 'tk'
require 'tkafter'
$running = nil
$acctime = 0
def load
if File.exists?('status.txt')
File.open('status.txt', 'r') do |f|
$acctime, $running = Marshal.load(f.read)
end
if $running
$start.configure('text' => 'Stop')
$after.start
else
$start.configure('text' => 'Start')
end
update_timer
end
end
def save
%(
File.open('status.txt', 'w') do |f|
f.write Marshal.dump([$acctime, $running])
end)
end
def start
if $running
$acctime += Time.now - $running
$start.configure('text' => 'Start')
$running = nil
$after.stop
update_timer
else
$running = Time.now
$start.configure('text' => 'Stop')
$after.start
end
save
end
def reset
$acctime = 0
$running = nil
$start.configure('text' => 'Start')
$after.stop
update_timer
end
def update_timer
time = $acctime
if $running
time += Time.now - $running
end
h = time.to_i / 3600
m = (time.to_i % 3600) / 60
s = time.to_i % 60
micro = ((time - time.to_i)*10).to_i
time = sprintf("%02i:%02i:%02i.%1i", h, m, s, micro)
$timer.configure('text' => time)
end
def setup_window
root = TkRoot.new {title "Timer"}
root.configure('background' => 'yellow')
f = TkFont.new('size' => 20, 'weight' => 'bold')
$timer = TkLabel.new(root) {
text '00:00:00.0'
font f
background 'yellow'
pack('pady' => 20, 'padx' => 20)
}
$start = TkButton.new(root) {
text 'Start'
pack('side'=>'left', 'expand'=>1, 'fill'=>'both')
command {start()}
}
TkButton.new(root) {
text 'Reset'
pack('expand'=> 1, 'side'=>'right', 'fill'=> 'both')
command {reset()}
}
end
setup_window
$after = TkAfter.new(100, -1, proc{update_timer})
#load
Tk.mainloop
b.jpg