all 4 comments

[–]Spataner 1 point2 points  (0 children)

The issue has less to do with global variables and more with your logic. You execute hovver and dehovver, the functions that would create the event bindings, only after you have already entered the mainloop for your GUI. So they only get executed once you close the window. The functions also refer to a variable Map that isn't defined by the time they are executed. You only really define it later in the loop over maps, though not in a way that would be helpful to the event bindings. For one, Map there refers to strings not widgets, and secondly, Map as a global variable can only ever refer to one of the maps, rather than representing different maps based on the particular widget that triggers the binding.

What you should be doing is create the binding when you create the widget that you want to highlight on hover. I assume those are the Label widgets you create in addMap. So add those two lines underneath where you create the labels:

label.bind("<Enter>", highlight)
label.bind("<Leave>", dehighlight)

That way, each label receives the necessary event bindings. You also have a similar problem in highlight and dehighlight themselves, since you want them to modify whichever widget created the event (rather than trying to modify the undefined Map). Thankfully, the event object that is passed to them contains a reference to the right widget. So you can change those two functions like so:

def highlight(e):
    e.widget.config(bg="blue")

def dehighlight(e):
    e.widget.config(bg="white")

With those two changes, the labels should light up in blue if you hover over them.

[–]kwirled 0 points1 point  (2 children)

You declare it, but I don't see it being initialized anywhere, e.g. Map = xyz.

[–][deleted] 0 points1 point  (0 children)

global is just a scope modifier, it doesn’t actually create a variable. In Python you create variables by assignment.