Little NumPad in tkinter Little NumPad in tkinter tkinter tkinter

Little NumPad in tkinter


Your lambda should be specified like this:

cmd= lambda b=b: print(b)

This will bind the current value of b to be the default value of the parameter named b in your lambda. Without this binding, the lambda will use the value of b at the time it is pressed rather than the value at the time it was created.

Personally I find it confusing to use the same variable in both contexts; I personally would write it like this, which is functionally identical but a little easier to understand:

cmd= lambda button=b: print(button)


cmd, your lambda, does not catch the value of b when the lambda itself was declared. the final state of b is 0, and that is why all your buttons end up printing b.


The for loop in NumPad.numpad_create loops over each of the button numbers and stores them in self.b. The problem is that each number overwrites the previous value of self.b, so that the only value that is actually stored is 0. tkinter automatically garbage collects objects that do not have a live reference. Add another attribute to NumPad which is a list and put all of your button objects into it.

@thkang is also right: I think that there are two separate bugs. Note that I haven't actually tested this.