menubu.tcl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # menubu.tcl --
  2. #
  3. # This demonstration script creates a window with a bunch of menus
  4. # and cascaded menus using menubuttons.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .menubu
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Menu Button Demonstration"
  13. wm iconname $w "menubutton"
  14. positionWindow $w
  15. frame $w.body
  16. pack $w.body -expand 1 -fill both
  17. if {[tk windowingsystem] eq "aqua"} {catch {set origUseCustomMDEF $::tk::mac::useCustomMDEF; set ::tk::mac::useCustomMDEF 1}}
  18. menubutton $w.body.below -text "Below" -underline 0 -direction below -menu $w.body.below.m -relief raised
  19. menu $w.body.below.m -tearoff 0
  20. $w.body.below.m add command -label "Below menu: first item" -command "puts \"You have selected the first item from the Below menu.\""
  21. $w.body.below.m add command -label "Below menu: second item" -command "puts \"You have selected the second item from the Below menu.\""
  22. grid $w.body.below -row 0 -column 1 -sticky n
  23. menubutton $w.body.right -text "Right" -underline 0 -direction right -menu $w.body.right.m -relief raised
  24. menu $w.body.right.m -tearoff 0
  25. $w.body.right.m add command -label "Right menu: first item" -command "puts \"You have selected the first item from the Right menu.\""
  26. $w.body.right.m add command -label "Right menu: second item" -command "puts \"You have selected the second item from the Right menu.\""
  27. frame $w.body.center
  28. menubutton $w.body.left -text "Left" -underline 0 -direction left -menu $w.body.left.m -relief raised
  29. menu $w.body.left.m -tearoff 0
  30. $w.body.left.m add command -label "Left menu: first item" -command "puts \"You have selected the first item from the Left menu.\""
  31. $w.body.left.m add command -label "Left menu: second item" -command "puts \"You have selected the second item from the Left menu.\""
  32. grid $w.body.right -row 1 -column 0 -sticky w
  33. grid $w.body.center -row 1 -column 1 -sticky news
  34. grid $w.body.left -row 1 -column 2 -sticky e
  35. menubutton $w.body.above -text "Above" -underline 0 -direction above -menu $w.body.above.m -relief raised
  36. menu $w.body.above.m -tearoff 0
  37. $w.body.above.m add command -label "Above menu: first item" -command "puts \"You have selected the first item from the Above menu.\""
  38. $w.body.above.m add command -label "Above menu: second item" -command "puts \"You have selected the second item from the Above menu.\""
  39. grid $w.body.above -row 2 -column 1 -sticky s
  40. ## See Code / Dismiss buttons
  41. set btns [addSeeDismiss $w.buttons $w]
  42. pack $btns -side bottom -fill x
  43. set body $w.body.center
  44. label $body.label -wraplength 300 -font "Helvetica 14" -justify left -text "This is a demonstration of menubuttons. The \"Below\" menubutton pops its menu below the button; the \"Right\" button pops to the right, etc. There are two option menus directly below this text; one is just a standard menu and the other is a 16-color palette."
  45. pack $body.label -side top -padx 25 -pady 25
  46. frame $body.buttons
  47. pack $body.buttons -padx 25 -pady 25
  48. tk_optionMenu $body.buttons.options menubuttonoptions one two three
  49. pack $body.buttons.options -side left -padx 25 -pady 25
  50. set m [tk_optionMenu $body.buttons.colors paletteColor Black red4 DarkGreen NavyBlue gray75 Red Green Blue gray50 Yellow Cyan Magenta White Brown DarkSeaGreen DarkViolet]
  51. if {[tk windowingsystem] eq "aqua"} {
  52. set topBorderColor Black
  53. set bottomBorderColor Black
  54. } else {
  55. set topBorderColor gray50
  56. set bottomBorderColor gray75
  57. }
  58. for {set i 0} {$i <= [$m index last]} {incr i} {
  59. set name [$m entrycget $i -label]
  60. image create photo image_$name -height 16 -width 16
  61. image_$name put $topBorderColor -to 0 0 16 1
  62. image_$name put $topBorderColor -to 0 1 1 16
  63. image_$name put $bottomBorderColor -to 0 15 16 16
  64. image_$name put $bottomBorderColor -to 15 1 16 16
  65. image_$name put $name -to 1 1 15 15
  66. image create photo image_${name}_s -height 16 -width 16
  67. image_${name}_s put Black -to 0 0 16 2
  68. image_${name}_s put Black -to 0 2 2 16
  69. image_${name}_s put Black -to 2 14 16 16
  70. image_${name}_s put Black -to 14 2 16 14
  71. image_${name}_s put $name -to 2 2 14 14
  72. $m entryconfigure $i -image image_$name -selectimage image_${name}_s -hidemargin 1
  73. }
  74. $m configure -tearoff 1
  75. foreach i {Black gray75 gray50 White} {
  76. $m entryconfigure $i -columnbreak 1
  77. }
  78. pack $body.buttons.colors -side left -padx 25 -pady 25
  79. if {[tk windowingsystem] eq "aqua"} {catch {set ::tk::mac::useCustomMDEF $origUseCustomMDEF}}