CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

How to add Index with integer and keep right click controls to it

Open KarthikBammidi3369 opened this issue 1 year ago • 7 comments

Hi,

I need information on how to create/Implement an Index number in frame with right click controls. image

Like above image when I click on Index numbers, i need to get the option to performance few operations.

Thanks in Advance.

KarthikBammidi3369 avatar Nov 20 '23 13:11 KarthikBammidi3369

@KarthikBammidi3369 You can use the simple tkinter popup menu like this: https://github.com/TomSchimansky/CustomTkinter/discussions/1739#discussioncomment-6230096

Or here is a modern popup menu you can use:

Screensho

from customtkinter import *
import sys

class CTkFloatingWindow(CTkToplevel):
    """
    On-screen popup window class for customtkinter
    Author: Akascape
    """
    def __init__(self,
                 master=None,
                 corner_radius=15,
                 border_width=1,
                 **kwargs):
        
        super().__init__(takefocus=1)
        
        self.focus()
        self.master_window = master
        self.corner = corner_radius
        self.border = border_width
        self.hidden = True

        # add transparency to suitable platforms
        if sys.platform.startswith("win"):
            self.after(100, lambda: self.overrideredirect(True))
            self.transparent_color = self._apply_appearance_mode(self._fg_color)
            self.attributes("-transparentcolor", self.transparent_color)
        elif sys.platform.startswith("darwin"):
            self.overrideredirect(True)
            self.transparent_color = 'systemTransparent'
            self.attributes("-transparent", True)
        else:
            self.attributes("-type", "splash")
            self.transparent_color = '#000001'
            self.corner = 0
            self.withdraw()
             
        self.frame = CTkFrame(self, bg_color=self.transparent_color, corner_radius=self.corner,
                              border_width=self.border, **kwargs)
        self.frame.pack(expand=True, fill="both")
        
        self.master.bind("<Button-1>", lambda event: self._withdraw(), add="+") # hide menu when clicked outside
        self.bind("<Button-1>", lambda event: self._withdraw()) # hide menu when clicked inside
        self.master.bind("<Configure>", lambda event: self._withdraw()) # hide menu when master window is changed
        
        self.resizable(width=False, height=False)
        self.transient(self.master_window)
         
        self.update_idletasks()
        
        self.withdraw()
        
    def _withdraw(self):
        if not self.hidden:
            self.withdraw()
            self.hidden = True
        
    def popup(self, x=None, y=None):
        self.x = x
        self.y = y
        self.deiconify()
        self.focus()
        self.geometry('+{}+{}'.format(self.x, self.y))
        self.hidden = False


"""
main application
"""

def do_popup(event, frame):
    """ open the popup menu """
    try: frame.popup(event.x_root, event.y_root)
    finally: frame.grab_release()
    
root = CTk()

float_window = CTkFloatingWindow(root) # our popup menu

widget = CTkLabel(root, text="Right Click Here")
widget.pack(padx=20, pady=20)

widget.bind("<Button-3>", lambda event: do_popup(event, float_window)) # right click mouse bind
widget.bind("<Button-2>", lambda event: do_popup(event, float_window)) # mac os right click (optional)

# Add menu buttons in float_window.frame

menu_button = CTkButton(float_window.frame, text="Click Here 1", fg_color="transparent", command=lambda: print("Hello"))
menu_button.pack(expand=True, fill="x", padx=10, pady=(10,0))

menu_button2 = CTkButton(float_window.frame, text="Click Here 2", fg_color="transparent", command=lambda: print("Hello"))
menu_button2.pack(expand=True, fill="x", padx=10, pady=(5,0))

menu_button3 = CTkButton(float_window.frame, text="Click Here 3", fg_color="transparent", command=lambda: print("Hello"))
menu_button3.pack(expand=True, fill="x", padx=10, pady=(5,10))

root.mainloop()

Akascape avatar Nov 22 '23 05:11 Akascape

@Akascape Nice one! Unfortunately the text color of the buttons does not match the 'Light' appearance mode: image

How can it be changed so that it is adjusted to two appearance modes (dark and light)?

DimaTepliakov avatar Nov 23 '23 13:11 DimaTepliakov

@DimaTepliakov Pass text_color=['black', 'white'] in the buttons.

Akascape avatar Nov 23 '23 13:11 Akascape

@Akascape Thanks a lot!

DimaTepliakov avatar Nov 23 '23 13:11 DimaTepliakov

@KarthikBammidi3369 You can use the simple tkinter popup menu like this: #1739 (comment)

Or here is a modern popup menu you can use:

Screensho

from customtkinter import *
import sys

class CTkFloatingWindow(CTkToplevel):
    """
    On-screen popup window class for customtkinter
    Author: Akascape
    """
    def __init__(self,
                 master=None,
                 corner_radius=15,
                 border_width=1,
                 **kwargs):
        
        super().__init__(takefocus=1)
        
        self.focus()
        self.master_window = master
        self.corner = corner_radius
        self.border = border_width
        self.hidden = True

        # add transparency to suitable platforms
        if sys.platform.startswith("win"):
            self.after(100, lambda: self.overrideredirect(True))
            self.transparent_color = self._apply_appearance_mode(self._fg_color)
            self.attributes("-transparentcolor", self.transparent_color)
        elif sys.platform.startswith("darwin"):
            self.overrideredirect(True)
            self.transparent_color = 'systemTransparent'
            self.attributes("-transparent", True)
        else:
            self.attributes("-type", "splash")
            self.transparent_color = '#000001'
            self.corner = 0
            self.withdraw()
             
        self.frame = CTkFrame(self, bg_color=self.transparent_color, corner_radius=self.corner,
                              border_width=self.border, **kwargs)
        self.frame.pack(expand=True, fill="both")
        
        self.master.bind("<Button-1>", lambda event: self._withdraw(), add="+") # hide menu when clicked outside
        self.bind("<Button-1>", lambda event: self._withdraw()) # hide menu when clicked inside
        self.master.bind("<Configure>", lambda event: self._withdraw()) # hide menu when master window is changed
        
        self.resizable(width=False, height=False)
        self.transient(self.master_window)
         
        self.update_idletasks()
        
        self.withdraw()
        
    def _withdraw(self):
        if not self.hidden:
            self.withdraw()
            self.hidden = True
        
    def popup(self, x=None, y=None):
        self.x = x
        self.y = y
        self.deiconify()
        self.focus()
        self.geometry('+{}+{}'.format(self.x, self.y))
        self.hidden = False


"""
main application
"""

def do_popup(event, frame):
    """ open the popup menu """
    try: frame.popup(event.x_root, event.y_root)
    finally: frame.grab_release()
    
root = CTk()

float_window = CTkFloatingWindow(root) # our popup menu

widget = CTkLabel(root, text="Right Click Here")
widget.pack(padx=20, pady=20)

widget.bind("<Button-3>", lambda event: do_popup(event, float_window)) # right click mouse bind
widget.bind("<Button-2>", lambda event: do_popup(event, float_window)) # mac os right click (optional)

# Add menu buttons in float_window.frame

menu_button = CTkButton(float_window.frame, text="Click Here 1", fg_color="transparent", command=lambda: print("Hello"))
menu_button.pack(expand=True, fill="x", padx=10, pady=(10,0))

menu_button2 = CTkButton(float_window.frame, text="Click Here 2", fg_color="transparent", command=lambda: print("Hello"))
menu_button2.pack(expand=True, fill="x", padx=10, pady=(5,0))

menu_button3 = CTkButton(float_window.frame, text="Click Here 3", fg_color="transparent", command=lambda: print("Hello"))
menu_button3.pack(expand=True, fill="x", padx=10, pady=(5,10))

root.mainloop()

Thank You @Akascape, I was able to create popup for each Line, but how to pass the Line no to the buttons.

karthik-bammidi avatar Nov 26 '23 09:11 karthik-bammidi

@karthik-bammidi

but how to pass the step no to the buttons.

Can you explain in details what exactly you want?

Akascape avatar Nov 26 '23 09:11 Akascape

@karthik-bammidi

but how to pass the step no to the buttons.

Can you explain in details what exactly you want?

I have a requirement to add steps in between a set of steps, like add step above step 5 or add step below step 5

image

It's like how we insert row in excel sheet.

karthik-bammidi avatar Nov 26 '23 09:11 karthik-bammidi