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?
#1
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?
Reply
#2
Change “lambda:” to “lambda i=i:” and it should work (sorry, I cannot test it now).
Reply
#3
Thank you, it is worked!
Reply
#4
Great! I actually just struggled with that same problem earlier today.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 818 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 746 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,380 May-25-2023, 07:37 PM
Last Post: deanhystad
Brick [Tkinter] [SOLVED] Password generation UnboundLocalError exception Milan 3 1,120 Nov-14-2022, 08:00 PM
Last Post: menator01
  Can't get tkinter button to change color based on changes in data dford 4 3,363 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,418 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,919 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,521 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,961 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,486 Jan-31-2021, 05:15 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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