0

Is it possible to automate button generation in tkinter?

I would like to write a simple image viewer. I have already written it, when my friend said she wanted nothing from the pictures to appear in the program, just buttons, and if she and if she clicks on that button, it will pop up the image. temp_row = -1 for i in range(len(image_list)): if i % 10 == 0 and i/10 > temp_row: temp_row += 1 Button(root, text="Image", command=lambda: open_pic(image_list[i])).grid(row=temp_row, column=(i % 10)) This code always opens the last item in the list, but can I fix it somehow?

10th Apr 2020, 12:56 AM
L贸g贸 P茅ter
L贸g贸 P茅ter - avatar
2 Answers
+ 5
L贸g贸 P茅ter inside the button command function, add a variable that will always be binder to "I". So, modify your lambda function to look like this. lambda y=I : open_pic( image_list[y]).
10th Apr 2020, 3:31 AM
Mirielle
Mirielle - avatar
0
Thank you, it is worked! :)
10th Apr 2020, 9:42 AM
L贸g贸 P茅ter
L贸g贸 P茅ter - avatar