Detecting `<Enter>` and `<Leave>` events for a frame in tkinter
<p>I have a menu which when the user moves their mouse off I want to disappear. The menu consists of a <code>Frame</code> packed with several <code>Label</code>/<code>Button</code> widgets. I can detect the user moving their mouse off a <code>Label</code>/<code>Button</code> with simply binding to the <code>&lt;Enter&gt;</code>/<code>&lt;Leave</code> events for those. But binding to the same events for the frame never triggers - I guess because the widgets on top cover up the frame so the mouse never enters it?</p>

<p>Is their a way to make the events propagate down to the containing <code>Frame</code>? </p>

<pre><code>window=tkinter.Tk()
menu_frm = tkinter.Frame(window, name="menu_frm")
lbl_1 = tkinter.Label(menu_frm, text="Label", name="lbl_1")
lbl_1.pack()
lbl_2 = tkinter.Label(menu_frm, text="Label", name="lbl_2")
lbl_2.pack()
menu_frm.pack()
# These two (per label and the number of labels is likely to grow) events fire
lbl_1.bind("&lt;Enter&gt;", lambda _: stayopenfunction())
lbl_1.bind("&lt;Leave&gt;", lambda _: closefunction())
lbl_2.bind("&lt;Enter&gt;", lambda _: stayopenfunction())
lbl_2.bind("&lt;Leave&gt;", lambda _: closefunction())
# These two events do not fire
menu_frm.bind("&lt;Enter&gt;", lambda _: stayopenfunction())
menu_frm.bind("&lt;Leave&gt;", lambda _: closefunction())
</code></pre>

<p>If you're just trying to simplify maintenance of the callbacks on the widgets, then I don't think binding events to the containing frame is the best approach.  (I guess it's possible to generate virtual events in the callbacks on the buttons and labels, and bind the virtual event to the frame, but it seems rather complicated.  Maybe that's just because I have no experience with virtual events.)</p>

<p>Why not just subclass the label and button widgets?  Look at the "Instance and Class Bindings" heading at <a href="http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm" rel="nofollow">http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm</a>  Then you'd just have two class binding to maintain, one for the labels and one for the buttons.  This seems cleaner to me; it's just vanilla OOP.</p>

<p>Hope this helps.</p>

python tkinter