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