0

I am trying to create a simple matching game using Python 2.7 and Tkinter.

The work-in-progress source code lives here.

My problem is this particular code snippet:

for i in range(16):
    self.img[i].config(command=lambda btn=self.btn[i]: \
            self.show_btn(self.btn[i]))
    self.btn[i].config(command=lambda btn=self.btn[i]: \
            self.hide_btn(self.btn[i]))

There is an existing logic error that I cannot seem to figure out.

The thing is it only configures the last two buttons that is, img[15] and btn[15].

By the way, img and btn are two sets of Button widgets stored in a list.

arantebw
  • 93
  • 2
  • 8
  • 1
    You should use the lambda argument `btn` instead of `self.btn[i]` inside the lambda functions. – acw1668 Nov 08 '16 at 09:28
  • @acw1668 I tried `btn=btn` but then I encountered a `NameError`. Using `btn=self.btn`, it did ran without error but then again the last 2 stacks of Button widgets only works. – arantebw Nov 08 '16 at 09:35
  • 1
    Not changing the argument, but using the `btn` inside the lambda function, like `self.img[i].config(command=lambda btn=self.btn[i]: self.show_btn(btn))`. – acw1668 Nov 08 '16 at 09:38
  • @acw1668 it works! Thank you! So, in a lambda we are creating a variable that will be accepted as an argument of a function. – arantebw Nov 08 '16 at 09:45

0 Answers0