Tkinter Menu

Tkinter Menu widget is used to create various types of menus in the Python GUI such as top-level menus, which are displayed right under the title bar of the parent window.

Syntax:

The Parameter master represents the parent window and options can be used as key-value pairs separated by commas.

Options of Tkinker Menu:

image: To display an image on this menubutton.

active foreground: Color of the text in the button when it gets clicked.

active background: Color of the background area of the button when it gets clicked.

selectcolor: Specifies the color displayed in checkbuttons and radiobuttons when they are selected.

activeborderwidth: Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.

bg: The initial background color of the button.

bd: border-width of the button.

tearoff: Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1.

If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.

font: The default font for textual choices.

cursor: The cursor that appears when the mouse is over the choices, but only when the menu has been torn off.

postcommand: You can set this option to a procedure, and that procedure will be called every time someone brings up this menu.

title: Normally, the title of a tear-off menu window will be the same as the text of the menu button or cascade that lead to this menu. If you want to change the title of that window, set the title option to that string.

disabledforeground: The color of the text for items whose state is DISABLED.

relief: The default 3-D effect for menus is relief=RAISED.

Tkinker Menu Functions:

add_command(options):

Adds menu items to the Menu.

add_checkbutton(options):

Adds a Check Button item to the Menu.

add_radiobutton(options):

Adds a radio button item to the Menu

add_seperator():

Adds a separator line in the menu to partition items.

index(item):

Returns the index of a specified Menu item.

add_cascade(options):

Used to introduce hierarchy into the Menu.

add(type, options):

Adds an item of a specified type to the Menu.

Example:
from tkinter import *

appWindow = Tk()
appWindow.title("Window Title-Tutorialsart.com")

appWindow.geometry('350x200')


def Some_Task():
    filewin = Toplevel(appWindow)
    button = Button(filewin, text="Button Title")
    button.pack()


main_menubar = Menu(appWindow)

File_Menu = Menu(main_menubar, tearoff=0)
main_menubar.add_cascade(label="File", menu=File_Menu)
File_Menu.add_command(label="New", command=Some_Task)
File_Menu.add_command(label="Open", command=Some_Task)


File_Menu.add_separator()

File_Menu.add_command(label="Exit", command=appWindow.quit)


Edit_Menu = Menu(main_menubar, tearoff=0)
main_menubar.add_cascade(label="Edit", menu=Edit_Menu)


Edit_Menu.add_command(label="Cut", command=Some_Task)
Edit_Menu.add_command(label="Copy", command=Some_Task)
Edit_Menu.add_command(label="Paste", command=Some_Task)

appWindow.config(menu=main_menubar)

appWindow.mainloop()
Output:
02 menu

Comments are closed.