TKinter Button widget

Neal Norwitz neal at metaslash.com
Sun Apr 21 11:27:53 EDT 2002


Peter Saffrey wrote:
> 
> Perhaps I should have said that I need this in an object-oriented
> context. It doesn't seem to work using an anonymous lambda function to
> a class method: example below.

That isn't a class method (2.2 terminology).  Here's a version which works.
You can do somethings a bit different in 2.2, but this should work back
to 1.5.2 at least.

Changes are in call to lambda and printNumber() takes self.

Neal
--
from Tkinter import *

class gui:
    def __init__(self):
        win = Frame()
        win.pack()
        Button(win, text='print 3', command=lambda self=self:self.printNumber(3)).pack()
        Button(win, text='print 5', command=lambda self=self:self.printNumber(5)).pack()
        win.mainloop()

    def printNumber(self, number):
        print number

if __name__ == '__main__':
    test = gui()



More information about the Python-list mailing list