Tk Source Code

View Ticket
Login
Ticket UUID: 873655
Title: Missing event when posting cascade menus
Type: Bug Version: obsolete: 8.4.5
Submitter: s_a_white Created on: 2004-01-09 09:45:08
Subsystem: 12. Unix Menus Assigned To: aku
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-10-17 13:31:57
Resolution: Fixed Closed By: nemethi
    Closed on: 2026-10-17 13:31:57
Description:
Here is an example of the problem found under Linux:

button .b
pack .b
.b configure -activebackground red
menu .m
menu .m.c
.m add cascade -menu ".m.c" -label "cascade"
.b configure -command {tk_popup .m [winfo rootx .b]
[expr [winfo rooty .b] + [winfo height .b]]; .m
activate 1; .m postcascade 1; tk_menuSetFocus .m}

Use the mouse and click on the button and now move the
mouse away.  You will see that the buttons state is not
restored to normal and is left as active.  Without the
postcascade as soon as you click on the button a
<Leave> event is generated causing the button state to
be restored to normal although the mouse is still
within the widget.  Both of these behaviours appear to
not be correct.

I am unable to test the same conditions using
menubutton as I can't see a way to post the cascade
within the same event execution.  However menubutton is
slightly different in that the <Leave> event is not
generated until the mouse moves outside the widget.
User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-10-17 13:31:57:

Merged by Jan into main and core-9-0-branch by commits [44e24602] and [a0aec1c3]; backported to core-8-6-branch by commit [20e2b202]. Closing the ticket.


serhiy.storchaka added on 2026-10-01 17:26:33:

Still reproducible in 8.6 and 9.x on X11. When tk_popup sets the grab, Tk generates a <Leave> event for the button, but it is processed only while postcascade waits for the cascade menu to be mapped. The grab is in effect by then, so TkPointerEvent drops the event as outside the grab. This is why "after idle .m postcascade 1" works around it.

Proposed fix in branch [cascade-grab-leave]: events generated by a grab change are always delivered. Test menu-45.1.


kjnash added on 2017-06-14 03:23:54:
I don't have a proper explanation of this bug, except that events are not arriving in the order that Tk expects.  A workaround that restores "correct" behaviour is to use "after idle" to delay the posting of ".m.c".

The call to tk_menuSetFocus is unnecessary, because this command is called by tk_popup.

button .b
pack .b -padx 100 -pady 100
.b configure -activebackground red
menu .m
menu .m.c
.m   add cascade -menu ".m.c" -label "cascade"
.m.c add command -label Hi!
.b configure -command {
    tk_popup .m [winfo rootx .b] [expr [winfo rooty .b] + [winfo height .b]]
    .m activate 1
    # tk_menuSetFocus .m
    after idle .m postcascade 1
}