CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

Enhancement: add active state color for button

Open felipetesc opened this issue 2 years ago • 2 comments

Hello there! I would like to propose the addition of the active state background color for buttons.

How to implement the feature:

This can be achieved by adding a property inside the constructor of CTkButton, like this:

def __init__(self, *args,
                 bg_color=None,
                 bg_active="default_theme", # this is new
                 fg_color="default_theme",
                 hover_color="default_theme",
                 border_color="default_theme",
                 border_width="default_theme",
                 command=None,
                 textvariable=None,
                 width=140,
                 height=28,
                 corner_radius="default_theme",
                 text_font="default_theme",
                 text_color="default_theme",
                 text_color_disabled="default_theme",
                 text="CTkButton",
                 hover=True,
                 image=None,
                 compound=tkinter.LEFT,
                 state=tkinter.NORMAL,
                 **kwargs):

By adding a simple command inside the constructor like this (I placed after the line self.border_color = ThemeManager.theme["color"]["button_border"] if border_color == "default_theme" else border_color):

self.active_color = ThemeManager.theme["color"]["button_active"] if bg_active == "default_theme" else bg_active

(I placed after the line self.border_color = ThemeManager.theme["color"]["button_border"] if border_color == "default_theme" else border_color)

And inside the method configure, as the last if clause verification, I included the code:

if "is_active" in kwargs:
            if kwargs["is_active"] is True:
                self.fg_color = self.active_color
            else:
                self.fg_color = ThemeManager.theme["color"]["button"]
            del kwargs["is_active"]
            require_redraw = True

Inside each theme file, I included the line: "button_active": ["#9b45ff", "#9b45ff"], after the button_hover property.

For now the active state background for the button has the same value as the hover background;

What it might solve:

We can use buttons to mimic a tabbed selection control

How can we use it:

By simply calling: mycool_bt.configure(is_active=True) or mycool_bt.configure(is_active=False)

Thanks!

felipetesc avatar Jul 25 '22 21:07 felipetesc

I can try my best to add this feature.

lmwenda avatar Aug 04 '22 10:08 lmwenda

I made a pull request introducing this feature. Hope it works fine thanks.

lmwenda avatar Aug 04 '22 12:08 lmwenda