Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Last active December 31, 2015 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alucky0707/8042272 to your computer and use it in GitHub Desktop.
Save alucky0707/8042272 to your computer and use it in GitHub Desktop.
勢いで作った艦これタイマー。自動で時間を設定する機能はありません。また、画像と音声は自分で探してきてください。 win32-soundに依存しているのでWindows限定です。
require 'tk'
require 'tkextlib/tkimg/png'
require 'tkextlib/tkimg/jpeg'
require "win32/sound"
include Win32
#表示したい画像(png,gif,jpegならいける)
img = TkPhotoImage.new file: "./girl.png"
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