1

I used Tkinter to make a context menu(right click menu), but when ever I just right click the window to open Context menu, all the functions associated with the labels.

from tkinter import *

def copy():
    print("Copied")


def copy_all():
    print("Copied all")


def paste():
    print("Paste")


def showContext(event):
    x, y = event.x_root, event.y_root
    ContextMenu = Menu(win, tearoff=0)
    ContextMenu.add_command(label="Copy  ", command=copy())
    ContextMenu.add_command(label="Copy All ", command=copy_all())
    ContextMenu.add_separator()
    ContextMenu.add_command(label="Paste", command=paste())
    ContextMenu.tk_popup(x,y)


win = Tk()
win.geometry("300x300")
win.bind("<Button-3>", showContext)

mainloop()

here is my code, when I just right click on the window the output is:

Copied
Copied All
Paste

Here is the image of the output

For me the function should be called only when I click any label on the menu

Praveen kumar
  • 26
  • 1
  • 3
  • "all the functions associated with the labels." what does this mean? – zr0gravity7 Dec 05 '21 at 07:56
  • @zr0gravity7 for example if i click right click, it is automatically executing all the three functions(copy, copy all and paste). But how i wanted it to work is, when I click copy option in the menu, only copy function should be called. – Praveen kumar Dec 05 '21 at 10:08

0 Answers0