How to access menu item from a menu

Asked

Viewed 322 times

2

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

The function bind may be useful, but if I don’t know how to access the item menu.

  • It’s a little late to ask but... the code I posted worked?

2 answers

2

You can use the method Menu.add_command to link a function to an item in the 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)

-2

Hi, thanks for your help, wanting to, just say I did the following:

def M_sg():
messagebox.showinfo('Msg', 'Dados')

and underneath, I wrote the call to function M_sg()

menubar.add_command(label = "Mensagem", command = root.M_sg)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.