Skip to content

Instantly share code, notes, and snippets.

@makenowjust
Last active August 29, 2015 13:57
Show Gist options
  • Save makenowjust/9449098 to your computer and use it in GitHub Desktop.
Save makenowjust/9449098 to your computer and use it in GitHub Desktop.
何のためとは言わないし言わせないタイマー。Ruby Installerからインストールしたなら動くはず
require 'tk'
require "win32/sound"
include Win32
#png, gifを表示したい場合はTcl/Tkの拡張ライブラリをインストールして、コメントを外して下さい
#require 'tkextlib/tkimg/png'
#require 'tkextlib/tkimg/jpeg'
#表示したい画像(gifならいける)
img = TkPhotoImage.new file: "./girl.gif"
w = img.width
h = img.height
#タイマー終了時に流したいwavファイル(短かいのにしないと死にます)
sound = "./sound.wav"
#タイトルとか
title = "さっき作った艦これタイマー"
root = TkRoot.new title: title
Tk::Wm.iconbitmap root, "kan.ico"
#画像表示用のキャンバス
canvas = TkCanvas.new width: w, height: h
TkcImage.new canvas, w/2, h/2, image: img
#タイマーの理由入力用のエントリー
txtFrm = Ttk::Frame.new
txtLbl = Ttk::Label.new(txtFrm, text: "どうしたの?").pack side: :left
txtEty = Ttk::Entry.new(txtFrm).pack fill: :x
#タイマーの時刻用のエントリー
timeFrm = Ttk::Frame.new
hourEty, minEty, secEty = (hour, min, sec = 3.times.map { TkVariable.new 0 }).map do|v|
Ttk::Entry.new timeFrm, textvariable: v, justify: 'right'
end
lbl = ->{ Ttk::Label.new timeFrm, text: ":" }
[hourEty, lbl[], minEty, lbl[], secEty].each do|w|
w.pack side: :left
end
#スタート/ストップボタン
button = Ttk::Button.new text: "start"
timer = nil
backup = 0, 0, 0
start = -> do
backup = [hour, min, sec].map(&:value).map(&:to_i)
#入力のチェック
if backup.all? {|v| v == 0 }
return
end
if backup.any? {|v| v < 0}
Tk.messageBox message: "入力値に負数があります!"
return
end
if backup[1..2].any? {|v| v >= 60 }
Tk.messageBox message: "分と秒は0以上59以下にしてください!"
return
end
button.text = "stop"
[hourEty, minEty, secEty].each do|e|
e.state = :disable
end
#タイマー
timer = TkAfter.new 1000, -1, it = ->(*_) {
h, m, s = [hour, min, sec].map(&:value).map(&:to_i)
s -= 1
if s == -1
s = 59
m -= 1
end
if m == -1
m = 59
h -= 1
end
if h == -1
root.title = title
button.text = "start"
timer.stop
Sound.play sound
h, m, s = backup
[hourEty, minEty, secEty].each do|e|
e.state = :active
end
else
root.title = "#{txtEty.value.empty? ? title : txtEty.value.encode("UTF-8")}[#{h}:#{m}:#{s}]"
end
[hour, min, sec].zip [h, m, s] do|var, val|
var.value = val
end
}
timer.start
it[]
end
stop = -> do
root.title = title
button.text = "start"
timer.stop
[hour, min, sec].zip backup do|var, val|
var.value = val
end
[hourEty, minEty, secEty].each do|e|
e.state = :active
end
end
button.command = -> do
case button.text
when "start"
start[]
when "stop"
stop[]
end
end
#ウィジェットの配置
canvas.pack
txtFrm.pack fill: :x
timeFrm.pack
button.pack
#メインループ(スレッド奪う)
Tk.mainloop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment