PageRenderTime 54ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Demo/tkinter/matt/menu-all-types-of-entries.py

https://bitbucket.org/glix/python
Python | 244 lines | 105 code | 55 blank | 84 comment | 0 complexity | 42c18962a05f41fd0b08689fcf3e7175 MD5 | raw file
  1. from Tkinter import *
  2. # some vocabulary to keep from getting confused. This terminology
  3. # is something I cooked up for this file, but follows the man pages
  4. # pretty closely
  5. #
  6. #
  7. #
  8. # This is a MENUBUTTON
  9. # V
  10. # +-------------+
  11. # | |
  12. #
  13. # +------------++------------++------------+
  14. # | || || |
  15. # | File || Edit || Options | <-------- the MENUBAR
  16. # | || || |
  17. # +------------++------------++------------+
  18. # | New... |
  19. # | Open... |
  20. # | Print |
  21. # | | <-------- This is a MENU. The lines of text in the menu are
  22. # | | MENU ENTRIES
  23. # | +---------------+
  24. # | Open Files > | file1 |
  25. # | | file2 |
  26. # | | another file | <------ this cascading part is also a MENU
  27. # +----------------| |
  28. # | |
  29. # | |
  30. # | |
  31. # +---------------+
  32. # some miscellaneous callbacks
  33. def new_file():
  34. print "opening new file"
  35. def open_file():
  36. print "opening OLD file"
  37. def print_something():
  38. print "picked a menu item"
  39. anchovies = 0
  40. def print_anchovies():
  41. global anchovies
  42. anchovies = not anchovies
  43. print "anchovies?", anchovies
  44. def makeCommandMenu():
  45. # make menu button
  46. Command_button = Menubutton(mBar, text='Simple Button Commands',
  47. underline=0)
  48. Command_button.pack(side=LEFT, padx="2m")
  49. # make the pulldown part of the File menu. The parameter passed is the master.
  50. # we attach it to the button as a python attribute called "menu" by convention.
  51. # hopefully this isn't too confusing...
  52. Command_button.menu = Menu(Command_button)
  53. # just to be cute, let's disable the undo option:
  54. Command_button.menu.add_command(label="Undo")
  55. # undo is the 0th entry...
  56. Command_button.menu.entryconfig(0, state=DISABLED)
  57. Command_button.menu.add_command(label='New...', underline=0,
  58. command=new_file)
  59. Command_button.menu.add_command(label='Open...', underline=0,
  60. command=open_file)
  61. Command_button.menu.add_command(label='Different Font', underline=0,
  62. font='-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
  63. command=print_something)
  64. # we can make bitmaps be menu entries too. File format is X11 bitmap.
  65. # if you use XV, save it under X11 bitmap format. duh-uh.,..
  66. Command_button.menu.add_command(
  67. bitmap="info")
  68. #bitmap='@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm')
  69. # this is just a line
  70. Command_button.menu.add('separator')
  71. # change the color
  72. Command_button.menu.add_command(label='Quit', underline=0,
  73. background='red',
  74. activebackground='green',
  75. command=Command_button.quit)
  76. # set up a pointer from the file menubutton back to the file menu
  77. Command_button['menu'] = Command_button.menu
  78. return Command_button
  79. def makeCascadeMenu():
  80. # make menu button
  81. Cascade_button = Menubutton(mBar, text='Cascading Menus', underline=0)
  82. Cascade_button.pack(side=LEFT, padx="2m")
  83. # the primary pulldown
  84. Cascade_button.menu = Menu(Cascade_button)
  85. # this is the menu that cascades from the primary pulldown....
  86. Cascade_button.menu.choices = Menu(Cascade_button.menu)
  87. # ...and this is a menu that cascades from that.
  88. Cascade_button.menu.choices.wierdones = Menu(Cascade_button.menu.choices)
  89. # then you define the menus from the deepest level on up.
  90. Cascade_button.menu.choices.wierdones.add_command(label='avacado')
  91. Cascade_button.menu.choices.wierdones.add_command(label='belgian endive')
  92. Cascade_button.menu.choices.wierdones.add_command(label='beefaroni')
  93. # definition of the menu one level up...
  94. Cascade_button.menu.choices.add_command(label='Chocolate')
  95. Cascade_button.menu.choices.add_command(label='Vanilla')
  96. Cascade_button.menu.choices.add_command(label='TuttiFruiti')
  97. Cascade_button.menu.choices.add_command(label='WopBopaLoopBapABopBamBoom')
  98. Cascade_button.menu.choices.add_command(label='Rocky Road')
  99. Cascade_button.menu.choices.add_command(label='BubbleGum')
  100. Cascade_button.menu.choices.add_cascade(
  101. label='Wierd Flavors',
  102. menu=Cascade_button.menu.choices.wierdones)
  103. # and finally, the definition for the top level
  104. Cascade_button.menu.add_cascade(label='more choices',
  105. menu=Cascade_button.menu.choices)
  106. Cascade_button['menu'] = Cascade_button.menu
  107. return Cascade_button
  108. def makeCheckbuttonMenu():
  109. global fred
  110. # make menu button
  111. Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus',
  112. underline=0)
  113. Checkbutton_button.pack(side=LEFT, padx='2m')
  114. # the primary pulldown
  115. Checkbutton_button.menu = Menu(Checkbutton_button)
  116. # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
  117. # are not supported correctly at present. You have to do all your application
  118. # work through the calback.
  119. Checkbutton_button.menu.add_checkbutton(label='Pepperoni')
  120. Checkbutton_button.menu.add_checkbutton(label='Sausage')
  121. Checkbutton_button.menu.add_checkbutton(label='Extra Cheese')
  122. # so here's a callback
  123. Checkbutton_button.menu.add_checkbutton(label='Anchovy',
  124. command=print_anchovies)
  125. # and start with anchovies selected to be on. Do this by
  126. # calling invoke on this menu option. To refer to the "anchovy" menu
  127. # entry we need to know it's index. To do this, we use the index method
  128. # which takes arguments of several forms:
  129. #
  130. # argument what it does
  131. # -----------------------------------
  132. # a number -- this is useless.
  133. # "last" -- last option in the menu
  134. # "none" -- used with the activate command. see the man page on menus
  135. # "active" -- the currently active menu option. A menu option is made active
  136. # with the 'activate' method
  137. # "@number" -- where 'number' is an integer and is treated like a y coordinate in pixels
  138. # string pattern -- this is the option used below, and attempts to match "labels" using the
  139. # rules of Tcl_StringMatch
  140. Checkbutton_button.menu.invoke(Checkbutton_button.menu.index('Anchovy'))
  141. # set up a pointer from the file menubutton back to the file menu
  142. Checkbutton_button['menu'] = Checkbutton_button.menu
  143. return Checkbutton_button
  144. def makeRadiobuttonMenu():
  145. # make menu button
  146. Radiobutton_button = Menubutton(mBar, text='Radiobutton Menus',
  147. underline=0)
  148. Radiobutton_button.pack(side=LEFT, padx='2m')
  149. # the primary pulldown
  150. Radiobutton_button.menu = Menu(Radiobutton_button)
  151. # and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
  152. # are not supported correctly at present. You have to do all your application
  153. # work through the calback.
  154. Radiobutton_button.menu.add_radiobutton(label='Republican')
  155. Radiobutton_button.menu.add_radiobutton(label='Democrat')
  156. Radiobutton_button.menu.add_radiobutton(label='Libertarian')
  157. Radiobutton_button.menu.add_radiobutton(label='Commie')
  158. Radiobutton_button.menu.add_radiobutton(label='Facist')
  159. Radiobutton_button.menu.add_radiobutton(label='Labor Party')
  160. Radiobutton_button.menu.add_radiobutton(label='Torie')
  161. Radiobutton_button.menu.add_radiobutton(label='Independent')
  162. Radiobutton_button.menu.add_radiobutton(label='Anarchist')
  163. Radiobutton_button.menu.add_radiobutton(label='No Opinion')
  164. # set up a pointer from the file menubutton back to the file menu
  165. Radiobutton_button['menu'] = Radiobutton_button.menu
  166. return Radiobutton_button
  167. def makeDisabledMenu():
  168. Dummy_button = Menubutton(mBar, text='Dead Menu', underline=0)
  169. Dummy_button.pack(side=LEFT, padx='2m')
  170. # this is the standard way of turning off a whole menu
  171. Dummy_button["state"] = DISABLED
  172. return Dummy_button
  173. #################################################
  174. #### Main starts here ...
  175. root = Tk()
  176. # make a menu bar
  177. mBar = Frame(root, relief=RAISED, borderwidth=2)
  178. mBar.pack(fill=X)
  179. Command_button = makeCommandMenu()
  180. Cascade_button = makeCascadeMenu()
  181. Checkbutton_button = makeCheckbuttonMenu()
  182. Radiobutton_button = makeRadiobuttonMenu()
  183. NoMenu = makeDisabledMenu()
  184. # finally, install the buttons in the menu bar.
  185. # This allows for scanning from one menubutton to the next.
  186. mBar.tk_menuBar(Command_button, Cascade_button, Checkbutton_button, Radiobutton_button, NoMenu)
  187. root.title('menu demo')
  188. root.iconname('menu demo')
  189. root.mainloop()