Python Forum

Full Version: Is it possible to automate button generation in tkinter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
Iknow this code always will open the last item in the list, but can I fix it somehow?
Change “lambda:” to “lambda i=i:” and it should work (sorry, I cannot test it now).
Thank you, it is worked!
Great! I actually just struggled with that same problem earlier today.