-1

I am reading a csv file with 51 rows and 4 columns. I have stored it in a 2D list of dimensions 51 X 4. My objective is to create 51 buttons and each time pass in the value of element at the list[i][1] of the list thus created as its argument. But, in this code the value of list[50][1] is getting passed in all the 51 buttons. How do I pass the required distinct list[i][1] value to each ith button as argument ?

Here is my code. Please find the bug in it, help is greatly appreciated. Thank you.

file = open('stocklist.csv', 'r')
reader = csv.reader(file)

stocks = []
for line in reader:
    w = line[0]
    x = line[1]
    y = line[2]
    z = line[3]

    stocks.append([w, x, y, z])

height = 51
width = 4

for i in range(height):
    b = tk.Button(f, text=stocks[i][1], command=lambda: open_link(stocks[i][1]))
    b.grid(row=i, column=1)
    print(b)
Amit Prafulla
  • 381
  • 2
  • 5

1 Answers1

1

You can use functool's partial (link):

b = tk.Button(f, text=stocks[i][1], command=partial(open_link,stocks[i][1])

ThijsW
  • 2,599
  • 15
  • 17