menu.tcl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # menu.tcl --
  2. #
  3. # This demonstration script creates a window with a bunch of menus
  4. # and cascaded menus using menubars.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .menu
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Menu Demonstration"
  13. wm iconname $w "menu"
  14. positionWindow $w
  15. label $w.msg -font $font -wraplength 4i -justify left
  16. if {[tk windowingsystem] eq "aqua"} {
  17. $w.msg configure -text "This window has a menubar with cascaded menus. You can invoke entries with an accelerator by typing Command+x, where \"x\" is the character next to the command key symbol. The rightmost menu can be torn off into a palette by selecting the first item in the menu."
  18. } else {
  19. $w.msg configure -text "This window contains a menubar with cascaded menus. You can post a menu from the keyboard by typing Alt+x, where \"x\" is the character underlined on the menu. You can then traverse among the menus using the arrow keys. When a menu is posted, you can invoke the current entry by typing space, or you can invoke any entry by typing its underlined character. If a menu entry has an accelerator, you can invoke the entry without posting the menu just by typing the accelerator. The rightmost menu can be torn off into a palette by selecting the first item in the menu."
  20. }
  21. pack $w.msg -side top
  22. set menustatus " "
  23. frame $w.statusBar
  24. label $w.statusBar.label -textvariable menustatus -relief sunken -bd 1 -font "Helvetica 10" -anchor w
  25. pack $w.statusBar.label -side left -padx 2 -expand yes -fill both
  26. pack $w.statusBar -side bottom -fill x -pady 2
  27. ## See Code / Dismiss buttons
  28. set btns [addSeeDismiss $w.buttons $w]
  29. pack $btns -side bottom -fill x
  30. menu $w.menu -tearoff 0
  31. set m $w.menu.file
  32. menu $m -tearoff 0
  33. $w.menu add cascade -label "File" -menu $m -underline 0
  34. $m add command -label "Open..." -command {error "this is just a demo: no action has been defined for the \"Open...\" entry"}
  35. $m add command -label "New" -command {error "this is just a demo: no action has been defined for the \"New\" entry"}
  36. $m add command -label "Save" -command {error "this is just a demo: no action has been defined for the \"Save\" entry"}
  37. $m add command -label "Save As..." -command {error "this is just a demo: no action has been defined for the \"Save As...\" entry"}
  38. $m add separator
  39. $m add command -label "Print Setup..." -command {error "this is just a demo: no action has been defined for the \"Print Setup...\" entry"}
  40. $m add command -label "Print..." -command {error "this is just a demo: no action has been defined for the \"Print...\" entry"}
  41. $m add separator
  42. $m add command -label "Dismiss Menus Demo" -command "destroy $w"
  43. set m $w.menu.basic
  44. $w.menu add cascade -label "Basic" -menu $m -underline 0
  45. menu $m -tearoff 0
  46. $m add command -label "Long entry that does nothing"
  47. if {[tk windowingsystem] eq "aqua"} {
  48. set modifier Command
  49. } elseif {[tk windowingsystem] eq "win32"} {
  50. set modifier Control
  51. } else {
  52. set modifier Meta
  53. }
  54. foreach i {A B C D E F} {
  55. $m add command -label "Print letter \"$i\"" -underline 14 \
  56. -accelerator $modifier+$i -command "puts $i"
  57. bind $w <$modifier-[string tolower $i]> "puts $i"
  58. }
  59. set m $w.menu.cascade
  60. $w.menu add cascade -label "Cascades" -menu $m -underline 0
  61. menu $m -tearoff 0
  62. $m add command -label "Print hello" \
  63. -command {puts stdout "Hello"} -accelerator $modifier+H -underline 6
  64. bind $w <$modifier-h> {puts stdout "Hello"}
  65. $m add command -label "Print goodbye" -command {\
  66. puts stdout "Goodbye"} -accelerator $modifier+G -underline 6
  67. bind $w <$modifier-g> {puts stdout "Goodbye"}
  68. $m add cascade -label "Check buttons" \
  69. -menu $w.menu.cascade.check -underline 0
  70. $m add cascade -label "Radio buttons" \
  71. -menu $w.menu.cascade.radio -underline 0
  72. set m $w.menu.cascade.check
  73. menu $m -tearoff 0
  74. $m add check -label "Oil checked" -variable oil
  75. $m add check -label "Transmission checked" -variable trans
  76. $m add check -label "Brakes checked" -variable brakes
  77. $m add check -label "Lights checked" -variable lights
  78. $m add separator
  79. $m add command -label "Show current values" \
  80. -command "showVars $w.menu.cascade.dialog oil trans brakes lights"
  81. $m invoke 1
  82. $m invoke 3
  83. set m $w.menu.cascade.radio
  84. menu $m -tearoff 0
  85. $m add radio -label "10 point" -variable pointSize -value 10
  86. $m add radio -label "14 point" -variable pointSize -value 14
  87. $m add radio -label "18 point" -variable pointSize -value 18
  88. $m add radio -label "24 point" -variable pointSize -value 24
  89. $m add radio -label "32 point" -variable pointSize -value 32
  90. $m add sep
  91. $m add radio -label "Roman" -variable style -value roman
  92. $m add radio -label "Bold" -variable style -value bold
  93. $m add radio -label "Italic" -variable style -value italic
  94. $m add sep
  95. $m add command -label "Show current values" \
  96. -command "showVars $w.menu.cascade.dialog pointSize style"
  97. $m invoke 1
  98. $m invoke 7
  99. set m $w.menu.icon
  100. $w.menu add cascade -label "Icons" -menu $m -underline 0
  101. menu $m -tearoff 0
  102. # Main widget program sets variable tk_demoDirectory
  103. image create photo lilearth -file [file join $tk_demoDirectory \
  104. images earthmenu.png]
  105. $m add command -image lilearth \
  106. -hidemargin 1 -command [list \
  107. tk_dialog $w.pattern {Bitmap Menu Entry} \
  108. "The menu entry you invoked displays a photoimage rather than\
  109. a text string. Other than this, it is just like any other\
  110. menu entry." {} 0 OK ]
  111. foreach i {info questhead error} {
  112. $m add command -bitmap $i -hidemargin 1 -command [list \
  113. puts "You invoked the $i bitmap" ]
  114. }
  115. $m entryconfigure 2 -columnbreak 1
  116. set m $w.menu.more
  117. $w.menu add cascade -label "More" -menu $m -underline 0
  118. menu $m -tearoff 0
  119. foreach i {{An entry} {Another entry} {Does nothing} {Does almost nothing} {Make life meaningful}} {
  120. $m add command -label $i -command [list puts "You invoked \"$i\""]
  121. }
  122. set emojiLabel [encoding convertfrom utf-8 "\xF0\x9F\x98\x8D Make friends"]
  123. $m add command -label $emojiLabel -command [list puts "Menu labels can include non-BMP characters."]
  124. $m entryconfigure "Does almost nothing" -bitmap questhead -compound left \
  125. -command [list \
  126. tk_dialog $w.compound {Compound Menu Entry} \
  127. "The menu entry you invoked displays both a bitmap and a\
  128. text string. Other than this, it is just like any other\
  129. menu entry." {} 0 OK ]
  130. set m $w.menu.colors
  131. $w.menu add cascade -label "Colors" -menu $m -underline 1
  132. menu $m -tearoff 1
  133. if {[tk windowingsystem] eq "aqua"} {
  134. # Aqua ignores the -background and -foreground options, but a compound
  135. # button can be used for selecting colors.
  136. foreach i {red orange yellow green blue} {
  137. image create photo image_$i -height 16 -width 16
  138. image_$i put black -to 0 0 16 1
  139. image_$i put black -to 0 1 1 16
  140. image_$i put black -to 0 15 16 16
  141. image_$i put black -to 15 1 16 16
  142. image_$i put $i -to 1 1 15 15
  143. $m add command -label $i -image image_$i -compound left -command [list \
  144. puts "You invoked \"$i\"" ]
  145. }
  146. } else {
  147. foreach i {red orange yellow green blue} {
  148. $m add command -label $i -background $i -command [list \
  149. puts "You invoked \"$i\"" ]
  150. }
  151. }
  152. $w configure -menu $w.menu
  153. bind Menu <<MenuSelect>> {
  154. global $menustatus
  155. if {[catch {%W entrycget active -label} label]} {
  156. set label " "
  157. }
  158. set menustatus $label
  159. update idletasks
  160. }