I have a for loop which iterates through a list. I have a counter that increases with every loop, which is supplied as the parameter. I want the counter to increase by 1 every loop where it is then supplied as the new parameter for that button command (similar to the original command where each button has a different value).
self.airfields_btn = []
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[0]), [260,375]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[1]), [205,270]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[2]), [120,377]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[3]), [170,295]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[4]), [265,355]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[5]), [70,240]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[6]), [180,400]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[7]), [160,420]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[8]), [188,225]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[9]), [155,185]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[10]), [135,142]))
self.airfields_btn.append(ImageButton(self, self.r_vfr, lambda: showinfo(icao_codes[11]), [205,360]))
dd_coords = {
1: (51.890329772, 0.442831562),
2: (54.21144, -1.29237),
3 : (51.8322, -4.7631),
4: (53.7719, -3.0370),
5: (52.2046, 0.1743),
6: (55.1582, -6.7915),
7: (51.6681, -2.0569),
8: (50.8600, -3.2347),
9: (55.2805, -1.7149),
10: (56.1833, -3.2203),
11: (57.5425, -4.0475),
12: (52.0408, -1.0956)
}
def get_location(self, index):
locx = dd_coords[index][0]
locy = dd_coords[index][1]
print(locx)
print(locy)
def set_vector(self):
index = 0
for items in self.airfields_btn:
items.config(command = lambda: self.get_location(index))
index = index + 1
However, when I run the code, index is 12 for all the buttons. How do I fix this?