Задача # 1
Чтобы lambda
сохранил текущее значение в итерации, его необходимо вызвать в лямбда-функции, такой как command= lambda f = file: selected_button(f)
.
Задача # 2
Метод, который я обычно использую для создания сетки кнопок, - это выбрать желаемую ширину, возможно, 3, а затем увеличивать столбец до тех пор, пока он не достигнет этой точки. Как только эта ширина будет достигнута, сбросьте столбец и увеличьте строку.
import tkinter as tk
# Testing
files = []
for x in range(12):
files.append((f"Test{x}", f"stuff{x}", f"other{x}"))
# /Testing
def 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 1
root.mainloop()
![Example](https://i.stack.imgur.com/dTwgx.png)