もし、ボタンが欲しかったら、
TkButton.new
とすれば良い。
もし、このボタンに後で操作を加えたいならば、
button = TkButton.new
というように、
〜を変数に入れておく。
これだけでは、画面に表示されないので、
button.pack
と、pack する。とりあえず、ボタンを表示する
スクリプトの全部を書くと、
#!/usr/local/bin/ruby require "tk" button = TkButton.new button.pack Tk.mainloop
#!/usr/local/bin/ruby
require "tk"
button = TkButton.new
button.configure('text' => 'Press me!')
button.pack
Tk.mainloop
#!/usr/local/bin/ruby
require "tk"
button = TkButton.new {
text 'Press me!'
}.pack
Tk.mainloop
command option?でボタンを作成したときか、packしたときに こうやって指定すれば良い。
imageとactiveimageを指定する
Tcl/Tk と同様に、packer, placer, grid を使用する。
widget.pack
#!/usr/local/bin/ruby
require "tk"
top2 = TkToplevel.new
frame = TkFrame.new(top2)
frame.pack
TkLabel.new(frame) {
text 'Left2'
pack('side' => 'left')
}
TkLabel.new(frame) {
text 'Right2'
pack('side' => 'right')
}
TkLabel.new(top2) {
text 'Bottom2'
pack('side' => 'bottom')
}
Tk.mainloop
シンプルなダイアログボックスは、TkDialog クラスを使えば良いでしょう。
もし、TkDialog クラスなどを使わないのであれば、
top = TkToplevel.newなどのように、TkToplevel クラスを使用します。
widget.bind('<keyname>', action)
or widget.bind '<keyname>', action
#!/usr/local/bin/ruby
require "tk"
items = ["One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve"]
scroll = TkScrollbar.new
box = TkListbox.new {
relief 'sunken'
# width -1
height 5
setgrid 'yes'
yscrollcommand proc{|idx| scroll.set *idx}
}.pack('side'=>'left','fill'=>'both','expand'=>'yes')
scroll.command(proc{|*args| box.yview *args})
scroll.pack('side'=>'right', 'fill'=>'y')
for i in items
box.insert 'end', i
end
Tk.mainloop
TkLabel.new {
bitmap 'bitmap-name'
}.pack('side' => 'top')
TkLabel.new {
bitmap ('@' + ['.','images','face.bmp'].join(File::Separator))
}.pack('side' => 'top')
#!/usr/local/bin/ruby
require "tk"
TkLabel.new {
text 'Main'
}.pack
$imggif = TkPhotoImage.new {
file '/usr/local/lib/tk4.2jp/demos/images/earth.gif'
}
TkLabel.new {
image $imggif
}.pack
TkButton.new {
text 'close'
command 'Tk.root.destroy'
}.pack('side' => 'left')
TkButton.new {
text 'exit'
command 'exit'
}.pack('side' => 'right')
Tk.mainloop
Tcl/Tk に準ずる
variable を指定。variable は、TkVariable クラスを使用。
#!/usr/local/bin/ruby
require "tk"
letters = TkVariable.new
TkRadioButton.new {
bitmap '@/usr/local/lib/tk4.2jp/demos/images/letters.bmp'
variable letters
value 'full'
}.pack
TkRadioButton.new {
bitmap '@/usr/local/lib/tk4.2jp/demos/images/noletter.bmp'
variable letters
value 'empty'
}.pack
Tk.mainloop
#!/usr/local/bin/ruby
require "tk"
TkLabel.new {
text 'Foo'
font 'fixed'
}.pack
Tk.mainloop
#!/usr/local/bin/ruby
require "tk"
def do_print(entered)
print "The string \"#{entered}\" was entered.\n"
end
$user_entry = TkVariable.new
entry = TkEntry.new {
textvariable $user_entry
}.pack
TkButton.new {
text '表示'
command proc { do_print $user_entry.value}
}.pack
Tk.mainloop
show というのを使用します。
$user_entry = TkVariable.new
entry = TkEntry.new {
textvariable $user_entry
show '*'
}.pack