2011年10月15日(土)
Ruby/Tk (その2) イベント
備忘録 | |
![]()
Ruby/Tk でイベントをハンドルするサンプルスクリプトです。
下のスクリプトを実行すると、ウィンドウに緑色の矩形が表示されます。
矩形上のマウス操作で発生したイベントを捕捉して X, Y 座標を表示します。
(終了は、矩形上でマウス第3ボタンでダブルクリックします)
#!/usr/bin/env ruby # -*- coding : utf-8 -*- require 'tk' TkRoot.new { geometry '640x480' } TkLabel.new { bg :Green width 20 height 10 %w[ Enter Leave Motion B1-Motion ButtonPress-1 ButtonRelease-1 Double-1 Triple-1 ].each do |event| bind event, proc {|x,y| puts "#{event} #{[x,y].inspect}" }, "%x %y" end bind("Control-Double-3") { exit } }.pack Tk.mainloop
コメント