TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.36k stars 1.07k forks source link

Request: Right click to bring up a context menu in CTk Entry Widgets doesn't work. #1311

Closed AlbertSolomon closed 1 year ago

AlbertSolomon commented 1 year ago

I have noticed that right-clicking in the Entry Widget does not work as expected. This means that users are unable to use the context menu to perform actions such as copying and pasting text. Instead, they have to use keyboard shortcuts (ctrl + c / ctrl + v ) which may not be intuitive for all users.

Expected behavior: When right-clicking in the Entry Widget, the context menu should appear, allowing users to perform actions such as copying and pasting text.

Actual behavior: Right-clicking in the Entry Widget does not produce the context menu.

Additional information: I have only tested this issue with the Entry Widget, but it may apply to other widgets as well. Please note that I am using version 5.0.3 of CTk on Windows 11.

ElectricCandlelight commented 1 year ago

Not really a Custom Tkinter issue since this is the same standard behaviour on TKinter.

Akascape commented 1 year ago

@AlbertSolomon This feature is not available in standard tkinter library, so you can't expect that customtkinter will have it by default.

But you can easily make a right click menu using tkinter menu widget. Here is a basic example:

import tkinter
import customtkinter

customtkinter.set_appearance_mode("Dark")

def do_popup(event, frame):
    try: frame.tk_popup(event.x_root, event.y_root)
    finally: frame.grab_release()

root = customtkinter.CTk()

entry_box = customtkinter.CTkEntry(root, width=200)
entry_box.grid(padx=20, pady=20)

RightClickMenu = tkinter.Menu(entry_box, tearoff=False, background='#565b5e', fg='white', borderwidth=0, bd=0)
RightClickMenu.add_command(label="Paste", command=lambda: entry_box.insert(tkinter.END, root.clipboard_get()))
RightClickMenu.add_command(label="Copy", command=lambda: root.clipboard_append(entry_box.get()))

entry_box.bind("<Button-3>", lambda event: do_popup(event, frame=RightClickMenu))
root.bind("<1>", lambda event: event.widget.focus_set())

root.mainloop()

Screenshot

AlbertSolomon commented 1 year ago

@Akascape I tried your implementation and it works like a charm. Thanks for your assistance.

FranzKol commented 11 months ago

It works just if you have a 3-button mouse. If you are i.e. on a mac with a 2-button mouse you need to change the binding to to make it working.

Akascape commented 11 months ago

@FranzKol Add both button bindings in that case: <Button-3> and <Button-2>

FranzKol commented 11 months ago

@FranzKol Add both button bindings in that case: <Button-3> and <Button-2>

Good idea, I just tried to make it working for my own cofiguration. But this will work on any 2- or 3-button mouse. Thank's for the advice.