How to access the menu item of a menu

2

I would like to know how to access an item from a menu, to connect it to a function or handler . For example, I would like to control the behavior of the application when I click in the item menu.

The function bind can be useful, but I do not know how to access the menu item.

    
asked by anonymous 19.10.2014 / 20:08

1 answer

1

You can use the Menu.add_command method to link a function to an item of Menu :

# -*- coding: utf-8 -*-
from tkinter import *

def foo():
    print ("Foo bar")

root = Tk()
menubar = Menu(root)

menubar.add_command(label = "Foo!", command = foo)
menubar.add_command(label = "Exit", command = root.quit)

root.config(menu = menubar)
    
19.10.2014 / 22:31