@ajlhimik

Kак сохранить нажатие кнопки(из массива кнопок) в ткинтер, почему сохраняется только последняя?

import calendar
import datetime
from functools import partial

def changeBut (event, but, i):
    but["bg"] = "green"
    p.append(i)
    print(i)
    print(p)
def left_click(event, i):
    print(but.text[i])
p = []
m = datetime.date.today()
dof = ['Vs DSdsada','DDDa aaAAAA']
a = calendar.monthrange(m.year, m.month)
root = Tk()
i = 0
s = a[1]+1
de = []
while i < s:
    if i == 0:
        Label(root, text = '').grid(row = 1, column = i+1)
        i+=1
        continue
    Label(root, text = str(i)).grid(row = 1, column = i+1)
    i+=1
i = 0
myrow = 0
while i<s:
    if i == 0:
        Label(root, text=dof[0]).grid(row = 2, column = i+1)
        i+=1
        continue
    but = Button(root, text=i, textvariable=i)
    but.bind("<Button-1>", lambda event, but=but: changeBut(event, but, i))
    #but.bind('<Button-1>', left_click(event, i))
    but.grid(row = 2, column = i+1)
    i+=1
root.mainloop()


в масcиве 'p' нужно сохранить 'textvariable' нажатой кнопки
у меня пока вот так: 5abe3f4f8ad92090700525.png
  • Вопрос задан
  • 264 просмотра
Пригласить эксперта
Ответы на вопрос 1
LaRN
@LaRN
Senior Developer
Так как переменная i не параметр лямбды, то после завершения цикла во всел лямбдах будет одно последнее значение i = 32
Попробуйте так:
but.bind("", lambda event, but=but, i = i: changeBut(event, but, i))
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы