Tkinter Widget command function call

Richard Chamberlain richard_chamberlainNOriSPAM at ntlworld.com.invalid
Fri Jun 16 09:04:39 EDT 2000


Hi Thomas,

This is not oop so you'll have to convert a little bit:

from Tkinter import *
root=Tk()

def buttonPressed(event):
    print event.widget
    if event.widget.cget('text')=='Hello':
        event.widget.configure(text='Press Me')
    else:
        event.widget.configure(text='Hello')

for i in range(0,10):
    myButton=Button(root,text="Press Me")
    myButton.bind('<Button>',buttonPressed)
    myButton.pack(side=TOP)

root.mainloop()

<EOF>

So basically instead of using command I'm binding an event (in
this case <Button>) to it and passing the method I want calling.

That way you get an event passed which you can use to get the
calling widget. Obviously you're not keeping any references for
the buttons around so this may not be all that useful but you
could get and set the configuration of them.

Richard

Got questions?  Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com




More information about the Python-list mailing list