Python Forum
[Tkinter] Is it possible to automate button generation in tkinter? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Is it possible to automate button generation in tkinter? (/thread-25733.html)



Is it possible to automate button generation in tkinter? - FirePepi - Apr-10-2020

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?


RE: Is it possible to automate button generation in tkinter? - Riddle - Apr-10-2020

Change “lambda:” to “lambda i=i:” and it should work (sorry, I cannot test it now).


RE: Is it possible to automate button generation in tkinter? - FirePepi - Apr-10-2020

Thank you, it is worked!


RE: Is it possible to automate button generation in tkinter? - Riddle - Apr-10-2020

Great! I actually just struggled with that same problem earlier today.