Popup Menu
#1739
Replies: 1 comment 4 replies
-
Hi @Danalfa, ScreenshotCodeimport tkinter
import customtkinter
def do_popup(event, frame):
""" open the popup menu """
try: frame.tk_popup(event.x_root, event.y_root)
finally: frame.grab_release()
def cut_text():
""" cut text operation """
copy_text()
try: textbox.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST)
except: pass
def copy_text():
""" copy text operation """
root.clipboard_clear()
try: root.clipboard_append(textbox.get(tkinter.SEL_FIRST, tkinter.SEL_LAST))
except: pass
def paste_text():
""" paste text operation """
try: textbox.insert(textbox.index('insert'), root.clipboard_get())
except: pass
def clear_text():
""" clears the textbox """
textbox.delete("1.0","end")
root = customtkinter.CTk()
# Add a right click tkinter menu
right_click_menu = tkinter.Menu(root, tearoff=False, bg='black', fg='white', activebackground="grey",
borderwidth=0, bd=0)
right_click_menu.add_command(label="cut", command=cut_text)
right_click_menu.add_command(label="copy", command=copy_text)
right_click_menu.add_command(label="paste", command=paste_text)
right_click_menu.add_command(label="clear", command=clear_text)
# Add a textbox
textbox = customtkinter.CTkTextbox(root, undo=True)
textbox.pack(fill="both", expand=True, padx=20, pady=20)
# Textbox bindings
textbox.bind("<Button-3>", lambda event: do_popup(event, right_click_menu))
textbox.bind("<Button-2>", lambda event: do_popup(event, right_click_menu))
root.mainloop() |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
is there any way to make a popup menu, with copy, paste and cut functions?
Beta Was this translation helpful? Give feedback.
All reactions