home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / tkinter / matt / menu-all-types-of-entries.py < prev    next >
Text File  |  1996-07-30  |  9KB  |  251 lines

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