Right-click menu disappearing too fast when running a GUI in Linux – Tkinter

I am currently working on a user interface and I have the following problem – I have a context menu appearing when the right button of the mouse is clicked – and it works perfectly fine when I run my interface in Windows, but when I run it in Linux and I try clicking the right button of the mouse – the menu disappears very quickly, I can only use it if I keep my finger on the button while choosing the option I need, and then carefully pressing it, using the left button. Here is the code I used for the right-click menu:

def rClicker(e):

try:

    def rClick_Select(e,apnd=0):
            e.widget.event_generate('<Control-a>')

    def rClick_Copy(e):
            e.widget.event_generate('<Control-c>')

    def rClick_Cut(e):
            e.widget.event_generate('<Control-x>')

    def rClick_Paste(e):
        e.widget.event_generate('<Control-v>')
        e.widget.focus()

    def rClick_Undo(e):
            e.widget.event_generate('<Control-z>')

    def rClick_Redo(e):
            e.widget.event_generate('<Control-y>')


    nclst=[

            (' Cut   ', lambda e=e: rClick_Cut(e)),
            (' Copy   ', lambda e=e: rClick_Copy(e)),
            (' Paste   ', lambda e=e: rClick_Paste(e)),
            (' Undo   ', lambda e=e: rClick_Undo(e)),
            (' Redo   ', lambda e=e: rClick_Redo(e)),
            (' Select All   ', lambda e=e: rClick_Select(e))
            ]

    rmenu = Menu(None, tearoff=0, takefocus=0)

    for (txt, cmd) in nclst:
        rmenu.add_command(label=txt, command=cmd)

    rmenu.tk_popup(e.x_root+40, e.y_root+10,entry="0")

except TclError:
    print ' - rClick menu, something wrong'
    pass

return "break"

def rClickbinder(r):

try:
    for b in [ 'Text', 'Entry', 'Listbox', 'Label']: #
        r.bind_class(b, sequence='<Button-3>',
                         func=rClicker, add='')
except TclError:
    print ' - rClickbinder, something wrong'
    pass



    #bind context menu to a specific element
txt.bind('<Button-3>',rClicker, add='')
    #or bind it to any Text/Entry/Listbox/Label element
    #rClickbinder(master)


Source: windows

Leave a Reply