1# image2.tcl -- 2# 3# This demonstration script creates a simple collection of widgets 4# that allow you to select and view images in a Tk label. 5# 6# RCS: @(#) $Id$ 7 8if {![info exists widgetDemo]} { 9 error "This script should be run from the \"widget\" demo." 10} 11 12package require Tk 13 14# loadDir -- 15# This procedure reloads the directory listbox from the directory 16# named in the demo's entry. 17# 18# Arguments: 19# w - Name of the toplevel window of the demo. 20 21proc loadDir w { 22 global dirName 23 24 $w.f.list delete 0 end 25 foreach i [lsort [glob -type f -directory $dirName *]] { 26 $w.f.list insert end [file tail $i] 27 } 28} 29 30# selectAndLoadDir -- 31# This procedure pops up a dialog to ask for a directory to load into 32# the listobx and (if the user presses OK) reloads the directory 33# listbox from the directory named in the demo's entry. 34# 35# Arguments: 36# w - Name of the toplevel window of the demo. 37 38proc selectAndLoadDir w { 39 global dirName 40 set dir [tk_chooseDirectory -initialdir $dirName -parent $w -mustexist 1] 41 if {[string length $dir] != 0} { 42 set dirName $dir 43 loadDir $w 44 } 45} 46 47# loadImage -- 48# Given the name of the toplevel window of the demo and the mouse 49# position, extracts the directory entry under the mouse and loads 50# that file into a photo image for display. 51# 52# Arguments: 53# w - Name of the toplevel window of the demo. 54# x, y- Mouse position within the listbox. 55 56proc loadImage {w x y} { 57 global dirName 58 59 set file [file join $dirName [$w.f.list get @$x,$y]] 60 if {[catch { 61 image2a configure -file $file 62 }]} then { 63 # Mark the file as not loadable 64 $w.f.list itemconfigure @$x,$y -bg \#c00000 -selectbackground \#ff0000 65 } 66} 67 68set w .image2 69catch {destroy $w} 70toplevel $w 71wm title $w "Image Demonstration #2" 72wm iconname $w "Image2" 73positionWindow $w 74 75label $w.msg -font $font -wraplength 4i -justify left -text "This demonstration allows you to view images using a Tk \"photo\" image. First type a directory name in the listbox, then type Return to load the directory into the listbox. Then double-click on a file name in the listbox to see that image." 76pack $w.msg -side top 77 78## See Code / Dismiss buttons 79set btns [addSeeDismiss $w.buttons $w] 80pack $btns -side bottom -fill x 81 82frame $w.mid 83pack $w.mid -fill both -expand 1 84 85labelframe $w.dir -text "Directory:" 86# Main widget program sets variable tk_demoDirectory 87set dirName [file join $tk_demoDirectory images] 88entry $w.dir.e -width 30 -textvariable dirName 89button $w.dir.b -pady 0 -padx 2m -text "Select Dir." \ 90 -command "selectAndLoadDir $w" 91bind $w.dir.e <Return> "loadDir $w" 92pack $w.dir.e -side left -fill both -padx 2m -pady 2m -expand true 93pack $w.dir.b -side left -fill y -padx {0 2m} -pady 2m 94labelframe $w.f -text "File:" -padx 2m -pady 2m 95 96listbox $w.f.list -width 20 -height 10 -yscrollcommand "$w.f.scroll set" 97scrollbar $w.f.scroll -command "$w.f.list yview" 98pack $w.f.list $w.f.scroll -side left -fill y -expand 1 99$w.f.list insert 0 earth.gif earthris.gif teapot.ppm 100bind $w.f.list <Double-1> "loadImage $w %x %y" 101 102catch {image delete image2a} 103image create photo image2a 104labelframe $w.image -text "Image:" 105label $w.image.image -image image2a 106pack $w.image.image -padx 2m -pady 2m 107 108grid $w.dir - -sticky ew -padx 1m -pady 1m -in $w.mid 109grid $w.f $w.image -sticky nw -padx 1m -pady 1m -in $w.mid 110grid columnconfigure $w.mid 1 -weight 1 111