Re: Tkinter: passing parameters to menu commands (looping througha list)

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

  1. I face a strange behavior when adding menu labels / command parameters
    from a list:

    ***
    This is the code that works but I wish to avoid - ATR and IN appear in
    the menu, and __Dec is called with ATR or IN depending on the choice
    ***
    l_dec.add_command(label = 'ATR', command= lambda: self.__Dec('ATR'))
    l_dec.add_command(label = 'IN', command= lambda: self.__Dec('IN'))

    ***
    This is the code that does not works but I wish I had - ATR and IN
    appear in the menu, but __Dec is always called with the last list item
    (IN in this case)

    ***
    l_dec_list = ['ATR','IN']

    for i in l_dec_list:
    l_dec.add_command(label = i, command= lambda: self.__Dec(i))



    Any clue ?

    Regards,

    Philippe


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

  2. Philippe C. Martin

    Michael Fuhr Guest

    "Philippe C. Martin" <> writes:

    > l_dec_list = ['ATR','IN']
    >
    > for i in l_dec_list:
    > l_dec.add_command(label = i, command= lambda: self.__Dec(i))


    Give the lambda an argument with a default:

    l_dec.add_command(label=i, command=lambda x=i: self.__Dec(x))

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Jan 7, 2005
    #2
    1. Advertising

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:
    3,947
    Thomas Stegen
    Jun 28, 2003
  2. Tim Stanka
    Replies:
    1
    Views:
    785
    Jeff Epler
    Aug 2, 2004
  3. Philippe C. Martin

    Tkinter: passing parameters to menu commands

    Philippe C. Martin, Jan 7, 2005, in forum: Python
    Replies:
    1
    Views:
    289
    Kent Johnson
    Jan 7, 2005
  4. Philippe C. Martin

    Re: Tkinter: passing parameters to menu commands

    Philippe C. Martin, Jan 7, 2005, in forum: Python
    Replies:
    1
    Views:
    1,109
    Kent Johnson
    Jan 8, 2005
  5. Replies:
    5
    Views:
    262
Loading...

Share This Page