1# -*- coding: utf-8 -*- 2# 3# widget demo 'load image' (called by 'widget') 4# 5 6# toplevel widget が存在すれば削除する 7if defined?($image2_demo) && $image2_demo 8 $image2_demo.destroy 9 $image2_demo = nil 10end 11 12# demo 用の toplevel widget を生成 13$image2_demo = TkToplevel.new {|w| 14 title('Image Demonstration #2') 15 iconname("Image2") 16 positionWindow(w) 17} 18 19base_frame = TkFrame.new($image2_demo).pack(:fill=>:both, :expand=>true) 20 21# label 生成 22msg = TkLabel.new(base_frame) { 23 font $font 24 wraplength '4i' 25 justify 'left' 26 text "このデモではTkの photo image を使用して画像を見ることができます。最初にエントリ内ににディレクトリ名を入れて下さい。次に下のリストボックスにこのディレクトリをロードするため、リターンを押してください。その後、画像を選択するためにリストボックスの中のファイル名をダブルクリックして下さい。" 27} 28msg.pack('side'=>'top') 29 30# frame 生成 31TkFrame.new(base_frame) {|frame| 32 TkButton.new(frame) { 33 #text '了解' 34 text '閉じる' 35 command proc{ 36 tmppath = $image2_demo 37 $image2_demo = nil 38 tmppath.destroy 39 } 40 }.pack('side'=>'left', 'expand'=>'yes') 41 42 TkButton.new(frame) { 43 text 'コード参照' 44 command proc{showCode 'image2'} 45 }.pack('side'=>'left', 'expand'=>'yes') 46 47}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m') 48 49# 変数生成 50$dirName = TkVariable.new([$demo_dir,'..','images'].join(File::Separator)) 51 52# image 生成 53$image2a = TkPhotoImage.new 54 55# ファイル名入力部 56TkLabel.new(base_frame, 'text'=>'ディレクトリ:')\ 57.pack('side'=>'top', 'anchor'=>'w') 58 59image2_e = TkEntry.new(base_frame) { 60 width 30 61 textvariable $dirName 62}.pack('side'=>'top', 'anchor'=>'w') 63 64TkFrame.new(base_frame, 'height'=>'3m', 'width'=>20)\ 65.pack('side'=>'top', 'anchor'=>'w') 66 67TkLabel.new(base_frame, 'text'=>'ファイル:')\ 68.pack('side'=>'top', 'anchor'=>'w') 69 70TkFrame.new(base_frame){|w| 71 s = TkScrollbar.new(w) 72 l = TkListbox.new(w) { 73 width 20 74 height 10 75 yscrollcommand proc{|first,last| s.set first,last} 76 } 77 s.command(proc{|*args| l.yview(*args)}) 78 l.pack('side'=>'left', 'expand'=>'yes', 'fill'=>'y') 79 s.pack('side'=>'left', 'expand'=>'yes', 'fill'=>'y') 80 #l.insert(0,'earth.gif', 'earthris.gif', 'mickey.gif', 'teapot.ppm') 81 l.insert(0,'earth.gif', 'earthris.gif', 'teapot.ppm') 82 l.bind('Double-1', proc{|x,y| loadImage $image2a,l,x,y}, '%x %y') 83 84 image2_e.bind 'Return', proc{loadDir l} 85 86}.pack('side'=>'top', 'anchor'=>'w') 87 88# image 配置 89[ TkFrame.new(base_frame, 'height'=>'3m', 'width'=>20), 90 TkLabel.new(base_frame, 'text'=>'画像:'), 91 # TkLabel.new(base_frame, 'image'=>$image2a) 92 Tk::Label.new(base_frame, 'image'=>$image2a) 93].each{|w| w.pack('side'=>'top', 'anchor'=>'w')} 94 95# メソッド定義 96def loadDir(w) 97 w.delete(0,'end') 98 Dir.glob([$dirName,'*'].join(File::Separator)).sort.each{|f| 99 w.insert('end',File.basename(f)) 100 } 101end 102 103def loadImage(img,w,x,y) 104 img.file([$dirName, w.get("@#{x},#{y}")].join(File::Separator)) 105end 106 107