I’m coding an app and I have a grid, whenever the mouse hovers on labels in the grid, it should change colour. It is detecting hover because a message does print on terminal on <Enter>
/<Leave>
. Here’s the code needed:
from tkinter import *
root = Tk()
grid_frame = Frame(root, bg = "#17191c")
ribbon = Frame(root, height = 70, bg = "#0b0b0d")
ribbon.pack(fill = "x", side = "top")
grid_frame.pack(fill = "both", expand = True)
class Grid:
def __init__(self):
block = PhotoImage("block.PNG")
hover = None
cells = []
def enter(index):
global hover
hover = index
cells[index].config(bg = "#272a2e")
print("enter")
def leave(index):
global hover
hover = index
cells[index].config(bg = "#17191c")
print("leave")
for x in range(0,101):
cells.append(Label(grid_frame, bg = "#17191c"))
row = 1
column = 1
for x in cells:
while row <= 10:
x.grid(row = row, column = column)
row += 1
else:
row = 1
column += 1
column += 1
for index, val in enumerate(cells):
val.bind("<Enter>", lambda event:enter(index))
val.bind("<Leave>", lambda event:leave(index))
One more thing: I want to fill the whole grid_frame
with Label
s, for this reason I put that long for
loop with a nested while
loop. However it only fills the top row, how do I fix that?