Tkinter: passing parameters to menu commands

Discussion in 'Python' started by Philippe C. Martin, Jan 7, 2005.

  1. Hi,

    I have he following need and do not find an easy way out:

    I have many menu items and would like them all to call the same method
    -However, I need the method called to react differently depending on the
    menu item selected. Since the menu command functions do not seem to
    receive any type of event style object, is there some type of Tkinter
    call that would let my method know the menu id selected ?

    Philippe




    --
    ***************************
    Philippe C. Martin
    SnakeCard LLC
    www.snakecard.com
    ***************************
     
    Philippe C. Martin, Jan 7, 2005
    #1
    1. Advertisements

  2. Philippe C. Martin

    Kent Johnson Guest

    Philippe C. Martin wrote:
    > I have many menu items and would like them all to call the same method
    > -However, I need the method called to react differently depending on the
    > menu item selected. Since the menu command functions do not seem to
    > receive any type of event style object, is there some type of Tkinter
    > call that would let my method know the menu id selected ?


    Much as it seems to be out of favor, IMO this is a place where a lambda expression is very handy.
    You can make a callback for each menu item that binds an extra parameter to the handler:

    # Based on an example by Fredrik Lundh
    from Tkinter import *

    def callback(code):
    print "called the callback with code", code

    root = Tk()

    # create a menu
    menu = Menu(root)
    root.config(menu=menu)

    filemenu = Menu(menu)
    menu.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="New", command=lambda: callback('New'))
    filemenu.add_command(label="Open...", command=lambda: callback('Open'))
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=lambda: callback('Exit'))

    mainloop()

    Of course you could do this with named forwarding functions if you prefer.

    Kent
     
    Kent Johnson, Jan 7, 2005
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    4,199
    Thomas Stegen
    Jun 28, 2003
  2. Tim Stanka
    Replies:
    1
    Views:
    962
    Jeff Epler
    Aug 2, 2004
  3. Philippe C. Martin

    Re: Tkinter: passing parameters to menu commands

    Philippe C. Martin, Jan 7, 2005, in forum: Python
    Replies:
    1
    Views:
    1,347
    Kent Johnson
    Jan 8, 2005
  4. Philippe C. Martin
    Replies:
    1
    Views:
    434
    Michael Fuhr
    Jan 7, 2005
  5. Harlin
    Replies:
    2
    Views:
    512
    Harlin Seritt
    Feb 23, 2005
  6. Matt S

    Cut paste Menu Commands

    Matt S, Nov 29, 2006, in forum: Java
    Replies:
    1
    Views:
    364
    Matt S
    Nov 29, 2006
  7. meATprivacyDOTnet

    parsing commands and parameters

    meATprivacyDOTnet, Apr 4, 2005, in forum: C Programming
    Replies:
    23
    Views:
    883
    CBFalconer
    Apr 11, 2005
  8. Ramon F Herrera
    Replies:
    8
    Views:
    706
    Pascal J. Bourguignon
    Sep 13, 2009
Loading...