Ruby/tk Widget

私は会社で、ちょっとしたプログラムにRubyを使用していますが、
GUIでtkを使ってみると、意外と簡単にできたものの、
Webでの資料がPerl等に比べ少ないです。
その辺りをメモ程度で纏めようと思います。

[参考URL]
ActiveTcl 8.5 Documentation
I love Programming
もっとTcl/Tk


  • HelloWorld(base)


require 'tk'

TkLabel.new(nil, :text=>"HelloWorld").pack

Tk.mainloop

以下requireとTk.mainloopを削除

  • Tk.messageBox


state=Tk.messageBox(:message=>"message", :type=>:yesno, :icon=>:question)
case state
when "yes"
#yesの場合の処理
when "no"
#noの場合の処理
end

  • TkButton


def temp
puts "temp"
end

TkButton.new(nil, :text=>'puts "open"', :command=>proc{puts "open"}).pack(:fill=>:x)
TkButton.new(nil, :text=>"def temp()", :command=>proc{temp()}).pack(:fill=>:x)
TkButton.new(nil, :text=>"close", :command=>proc{exit()}).pack(:fill=>:x)

method

  1. :text=>""
  2. :height=>高さ
  3. :width=>幅
  4. :command=>
  • TkCheckButton

TkCheckButton.new(nil, :text=>"ruby", :command=>proc{puts "ruby"}).pack(:side=>:left)
TkCheckButton.new(nil, :text=>"python", :command=>proc{puts "python"}).pack(:side=>:left)
TkCheckButton.new(nil, :text=>"perl", :command=>proc{puts "perl"}).pack(:side=>:left)


method

  • TkListbox
  • TkScrollbar


module ExtendListBox
def set(list, array)
array.each_index{|i|
list.insert(i,array[i])
}
end
def get(list, array)
return list.get(0,list.size)
end
def del(list)
list.delete(0,list.size)
end
end

include ExtendListBox

TkFrame.new{|f|
scr_x = TkScrollbar.new(f).pack(:side=>:bottom, :fill=>:x)
scr_y = TkScrollbar.new(f).pack(:side=>:right, :fill=>:y)
list = TkListbox.new(f).pack(:side=>:right, :fill=>:x)
a = [1,2,3,4,5,6,7,8,9,10]
set(list,a)
list.xscrollbar( scr_x )
list.yscrollbar( scr_y )
}.pack(:fill=>:y)


  • TkMenubar


menu_spec=[
]
TkMenubar.new(nil, menu_spec, :tearoff=>false)

  • Tk.getOpenFile


Tk.getOpenFile(:filetypes=>filetypes, :parent=>self)

  • template


#source


method

  • template


#source


method

  • template


#source

プログラミングRuby 第2版 言語編

プログラミングRuby 第2版 言語編