Tkinter List of Variable Sizes
Problem #1
To get the lambda
to store the current value in the iteration it needs to be called out in the lambda function such as command= lambda f = file: selected_button(f)
.
Problem #2
The method I would usually use for making a grid of buttons is pick a width you want, possibly 3 wide, then increment the column until it reaches that point. Once that width is reached, reset the column and increment the row.
import tkinter as tk# Testingfiles = []for x in range(12): files.append((f"Test{x}", f"stuff{x}", f"other{x}"))# /Testingdef selected_button(file): print(f"File: {file[0]}, {file[1]}, Link: {file[2]}")root = tk.Tk()r, c = (0,0) # Set the row and column to 0.c_limit = 3 # Set a limit for how wide the buttons should go.for file in files: tk.Button(root, text= file[0], width = 20, # This will store what is currently in file to f # then assign it to the button command. command= lambda f=file: selected_button(f) ).grid(row = r, column = c) c += 1 # increment the column by 1 if c == c_limit: c = 0 # reset the column to 0 r += 1 # increment the row by 1root.mainloop()