tkinter button command

Discussion in 'Python' started by Ajay, Aug 4, 2004.

  1. Ajay

    Ajay Guest

    hi!

    if i set two buttons to call the same function when they are pressed, is
    there any way, within the function of knowing which button invoked it.

    cheers

    --
    Ajay Brar,
    CS Honours 2004
    Smart Internet Technology Research Group





    ----------------------------------------------------------------
    This message was sent using IMP, the Internet Messaging Program.
     
    Ajay, Aug 4, 2004
    #1
    1. Advertising

  2. On Wed, 4 Aug 2004, Ajay wrote:

    > if i set two buttons to call the same function when they are pressed, is
    > there any way, within the function of knowing which button invoked it.


    Not built-in to Tkinter, but you can easily get such behaviour using
    lambda like so:

    >>> from Tkinter import *
    >>> def click(which):

    .... print which, 'was clicked!'
    >>> button1 = Button(text='Button 1',command=lambda: click('number 1'))
    >>> button2 = Button(text='Button 2',command=lambda: click('number 2'))
    >>> button1.pack()
    >>> button2.pack()


    lambda: will wrap the call to click() up in a function. You can call
    click() with any arguments this way; instead of strings, you could use
    integers or a reference to the object itself (though you'd have to set
    command after creating the button to do this).
     
    Christopher T King, Aug 4, 2004
    #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. Replies:
    0
    Views:
    502
  2. Replies:
    2
    Views:
    3,776
    Russell E. Owen
    Jan 8, 2004
  3. Paul A. Wilson

    Tkinter Button command query

    Paul A. Wilson, Apr 19, 2004, in forum: Python
    Replies:
    2
    Views:
    590
    Fredrik Lundh
    Apr 19, 2004
  4. Replies:
    8
    Views:
    548
    Francesco Bochicchio
    Nov 1, 2005
  5. Jay
    Replies:
    9
    Views:
    552
Loading...

Share This Page