Background:
A GUI table having a "skip combobox" widget allowing user to skip current line from executing. Amount of lines in this GUI varies. This "skip" is created using a for loop.
When selecting "on" or "off" a bind
process executes a method called self.but_callback
which doing the rest of code.
goal:
Pass i
value to self.but_callback
, along with event
needed to bind
.
Code below, shows a try to pass i
value directly into self.but_callback(event,i)
BUT instead of assigning the right i
value it passes the last value of for loop for every skip_button
created.
Question: How to pass correct i
value while in a loop, when 2 parameters are need to pass using lambda
function.
Did not find any answer combining both issues.
for i in range(len(data_from_file)):
#Skip button
self.var.append(tk.StringVar())
self.var[7].set('On')
skip_button = ttk.Combobox(inner_frame, width=5, textvariable=self.var[7], values=['On','Off'],state='readonly', justify=tk.CENTER)
skip_button.bind('<<ComboboxSelected>>',lambda event: self.but_callback(event,i))
skip_button.grid(row=i+1, column=7, padx=8)
def but_callback(self,event,x):
print(x)
See Question&Answers more detail:os