Python3 Tkinter popup menu doesn't close automatically when clicked elsewhere

I am running Python 3.3.3 (and now I'm on Ubuntu, but I'm also developing on Mac and Windows that I haven't tested yet). I have a Treeview object that responds to the right click on the elements and shows the context menu depending on what you click ... but I noticed that if you right-click in another place and the original menu will open , it will simply open another.

In fact, a normal click does not hide them. Even when I close the window, the menu still remains floating. The only way to get them to leave is to click on one of the options.

The end result is as follows: Context Menus EVERYWHERE

My menu code is as follows:

def rightclick_listitem(self, event):
    rowitem = self.sources.identify('item', event.x, event.y)

    if rowitem == '':
        print('Right clicked an empty space.')
        return
    # user right clicked something.
    self.sources.selection_set(rowitem)
    rcmenu = Menu(self.root, tearoff=0)
    plugin_disabled=self.sources.item(rowitem, 'values')[0] == 'Disabled'
    if plugin_disabled:
        rcmenu.add_command(label='Plugin is disabled...',
                           command=self.plugin_disabled_click)
    rcmenu.add_command(label='Plugin options',state='disabled' if plugin_disabled else 'active')
    rcmenu.add_command(label='Uninstall plugin')
    rcmenu.post(event.x_root, event.y_root)

The code that calls this code is here:

    #RIGHTMOUSE is a variable that changes based on OS due to the way Mac OSX works
    #sources is the treeview object
    self.sources.bind(RIGHTMOUSE, self.rightclick_listitem)

googled . tkinter python , . .

, : https://github.com/Mgamerz/Fresh-Set-of-Images (freshsetofimages.py)

.

, : https://github.com/Mgamerz/fsoi_plugins

+3
2

tk_popup, post.

, , , , , . , , .

+3

, ,

rcmenu.bind("<FocusOut>",popupFocusOut)

unpost popupFocusOut.

def popupFocusOut(self,event=None):
        rcmenu.unpost()
0

Source: https://habr.com/ru/post/1674606/


All Articles