Tkinter Widgets-Menu

In Python, menu is generally used to provide a more than one option. We can create a menu form of popup and dropdown etc.
Syntax-

w = Menu ( main, option)
Parameter-
Main- parent Window.
Option- attribute of button widget.

Example-
  1. from tkinter import *    
  2. def none():    
  3.     filewin = Toplevel(main)    
  4.     button = Button(filewin, text="none")    
  5.     button.pack()    
  6. main = Tk()    
  7. menubar = Menu(main)    
  8. filemenu = Menu(menubar, tearoff=0)    
  9. filemenu.add_command(label="New", command=none)    
  10. filemenu.add_command(label="Open", command=none)    
  11. filemenu.add_command(label="Save", command=none)    
  12. filemenu.add_command(label="Save as...", command=none)    
  13. filemenu.add_command(label="Close", command=none)    
  14. filemenu.add_separator()    
  15. filemenu.add_command(label="Exit", command=main.quit)    
  16. menubar.add_cascade(label="File", menu=filemenu)    
  17. editmenu = Menu(menubar, tearoff=0)    
  18. editmenu.add_command(label="Undo", command=none)    
  19. editmenu.add_separator()    
  20. editmenu.add_command(label="Cut", command=none)    
  21. editmenu.add_command(label="Copy", command=none)    
  22. editmenu.add_command(label="Paste", command=none)    
  23. editmenu.add_command(label="Delete", command=none)    
  24. editmenu.add_command(label="Select All", command=none)    
  25. menubar.add_cascade(label="Edit", menu=editmenu)    
  26. helpmenu = Menu(menubar, tearoff=0)    
  27. helpmenu.add_command(label="Help Index", command=none)    
  28. helpmenu.add_command(label="About...", command=none)    
  29. menubar.add_cascade(label="Help", menu=helpmenu)    
  30. main.config(menu=menubar)    
  31. main.mainloop()    
Output


Option
Description
activebackground Sets background color of widgets, when the button is under the cursor.
activeborderwidth
It is used to define the border width.
activeforeground
It sets foreground color of widgets, when the button is under the cursor.
bg
Normal background color.
bd
It sets border width in pixels. Default is 1.
cursor
Cursor is used to create a cursor in canvas like arrow, circle, dot etc.
disabledforeground
It is used to disable foreground color of the text.
font
It is used to set the font of the text.
fg
It Setus normal foreground (text) color of widgets.
postcommand
It is used to set the postcommand of widgets.
relief
Relief is special type of border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
image
It is used to set an image on widgets.
selectcolor
It is used to set color of the selected item.
Summary

In this chapter, you learnt what menu widget is and how to use the property of the widget.
Next » Tkinter Widget-Message