Is there a simple way to get the right click menu to open on texty only and not the whole window?
This was a quick mashup to illustrate my question. Inheriting from texty on line 25 was a shot in the dark, which didnt work, but it’s close to a simple solution, like I am seeking. I was hoping to avoid programming a whole class each time I want to set a right click menu.
from tkinter import * from tkinter import ttk def menu_popup(event): try: popup.tk_popup(event.x_root, event.y_root, 0) finally: popup.grab_release() win = Tk() win.geometry("600x550+125+125") e = Entry(win, width=50, font=('Helvetica', 11)) e.pack() e.insert(0, "Some text....") label = Label(win, text="Right-click to see a menu", font= ('Helvetica 18')) label.pack(pady= 40) texty=Text(win, height=10) texty.pack() popup = Menu(texty, tearoff=0) popup.add_command(label="New") popup.add_separator() popup.add_command(label="Open") popup.add_separator() popup.add_command(label="Close") win.bind("<Button-3>", menu_popup) button = ttk.Button(win, text="Quit", command=win.destroy) button.pack() mainloop()
The widget on which the callback should be executed for the respective event is determined by the widget you call
bind on(and the level of bind too*). So if you want the event to be identified within
texty, then apply binding to it.
* There is
bind_all which executes no matter which widget has focus or is called upon. Read 54.1. Levels of binding for more info.