0

How to close(withdraw) 'tk_popup' menu (context menu) without explicitly invoking the menu item (choice)? I tried the following:

class CtxMenuMixin:
    def layout(self, popup=None):
        self.popup = popup
        self.popup.bind('<FocusOut>', self.on_focusout_popup)
        self.bind('<3>', self.on_click3)

    def on_focusout_popup(self, event=None):
        self.popup.unpost()

    def on_click3(self, event):
        self.click3_event = event
        try:
            self.popup.tk_popup(event.x_root, event.y_root, 0)
        finally:
            self.popup.grab_release() 

Where

popup = tk.Menu(self, tearoff=0)

It doesn't work. When the focus goes elsewhere, the menu continues to be visible. popup doesn't close

using Python 3.9.2, Debian GNU/Linux 11 (bullseye)

EDIT Found a workaround python-list/2002-September/161459.html which uses .post method instead of .tk_popup Making a choice is less convenient than with .tk_popup. I can dismiss the menu by clicking elsewhere.

class CtxMenuMixin:
    # Skip 
    def on_focusout_popup(self, event=None):
        self.popup.unpost()

    def on_click3(self, event):
        self.click3_event = event
        try:
            self.popup.post(event.x_root, event.y_root)
            self.popup.focus_set()
        finally:
            self.popup.grab_release()

0 Answers0