Python Forum
[Tkinter] Is it possible to automate button generation in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] 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))
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
Brick [Tkinter] [SOLVED] Password generation UnboundLocalError exception Milan 3 561 Nov-14-2022, 08:00 PM
Last Post: menator01
  Can't get tkinter button to change color based on changes in data dford 4 2,356 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 3,656 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 3,149 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 4,232 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,069 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 2,454 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 12,259 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 3,001 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 13,309 Oct-18-2020, 10:58 AM
Last Post: TurboC

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020