I'm trying to create 18 by 18 grid of labels and make each label have an Enter
and Leave
event. But when I write the code it only creates an event for the last lable in the grid. What am I not getting here?
p.s. Sorry if the code is messy, I'm only 1 month into learning Python
from tkinter import *
import string
root = Tk()
sequence_lst = list(string.ascii_letters)
execute = 0
num = 2
while execute < 3:
for i in range(len(sequence_lst)):
sequence_lst.append(sequence_lst[i]*num)
execute += 1
num += 1
sequence_lst = sequence_lst[:324]
position_x = 0
position_y = 0
square_lst = []
while position_x < 18:
for i in range(18):
if position_y < 18:
square = Label(root, width=2, borderwidth=1, relief='solid')
square.grid(row=position_x, column=position_y)
position_y += 1
square_lst.append(square)
else:
position_y = 0
position_x += 1
for sequence in sequence_lst:
sequence = square_lst[sequence_lst.index(sequence)]
sequence.bind('<Enter>', lambda event: sequence.configure(bg='blue'))
sequence.bind('<Leave>', lambda event: sequence.configure(bg='white'))
root.mainloop()