filebox.tcl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # filebox.tcl --
  2. #
  3. # This demonstration script prompts the user to select a file.
  4. if {![info exists widgetDemo]} {
  5. error "This script should be run from the \"widget\" demo."
  6. }
  7. package require Tk
  8. set w .filebox
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "File Selection Dialogs"
  12. wm iconname $w "filebox"
  13. positionWindow $w
  14. ttk::frame $w._bg
  15. place $w._bg -x 0 -y 0 -relwidth 1 -relheight 1
  16. ttk::label $w.msg -font $font -wraplength 4i -justify left -text "Enter a file name in the entry box or click on the \"Browse\" buttons to select a file name using the file selection dialog."
  17. pack $w.msg -side top
  18. ## See Code / Dismiss buttons
  19. set btns [addSeeDismiss $w.buttons $w]
  20. pack $btns -side bottom -fill x
  21. foreach i {open save} {
  22. set f [ttk::frame $w.$i]
  23. ttk::label $f.lab -text "Select a file to $i: " -anchor e
  24. ttk::entry $f.ent -width 20
  25. ttk::button $f.but -text "Browse ..." -command "fileDialog $w $f.ent $i"
  26. pack $f.lab -side left
  27. pack $f.ent -side left -expand yes -fill x
  28. pack $f.but -side left
  29. pack $f -fill x -padx 1c -pady 3
  30. }
  31. if {[tk windowingsystem] eq "x11"} {
  32. ttk::checkbutton $w.strict -text "Use Motif Style Dialog" \
  33. -variable tk_strictMotif -onvalue 1 -offvalue 0
  34. pack $w.strict -anchor c
  35. # This binding ensures that we don't run the rest of the demos
  36. # with motif style interactions
  37. bind $w.strict <Destroy> {set tk_strictMotif 0}
  38. }
  39. proc fileDialog {w ent operation} {
  40. # Type names Extension(s) Mac File Type(s)
  41. #
  42. #---------------------------------------------------------
  43. set types {
  44. {"Text files" {.txt .doc} }
  45. {"Text files" {} TEXT}
  46. {"Tcl Scripts" {.tcl} TEXT}
  47. {"C Source Files" {.c .h} }
  48. {"All Source Files" {.tcl .c .h} }
  49. {"Image Files" {.gif} }
  50. {"Image Files" {.jpeg .jpg} }
  51. {"Image Files" "" {GIFF JPEG}}
  52. {"All files" *}
  53. }
  54. if {$operation == "open"} {
  55. global selected_type
  56. if {![info exists selected_type]} {
  57. set selected_type "Tcl Scripts"
  58. }
  59. set file [tk_getOpenFile -filetypes $types -parent $w \
  60. -typevariable selected_type]
  61. puts "You selected filetype \"$selected_type\""
  62. } else {
  63. set file [tk_getSaveFile -filetypes $types -parent $w \
  64. -initialfile Untitled -defaultextension .txt]
  65. }
  66. if {[string compare $file ""]} {
  67. $ent delete 0 end
  68. $ent insert 0 $file
  69. $ent xview end
  70. }
  71. }