image2.tcl 3.3 KB

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