button state functionality

This commit is contained in:
Tom Schimansky 2021-07-23 00:44:13 +02:00
parent bd10b5612e
commit ee5a19e55d
5 changed files with 164 additions and 45 deletions

View File

@ -158,6 +158,7 @@ text_color | text color, tuple: (light_color, dark_color) or single color
text_font | button text font, tuple: (font_name, size)
hover | enable/disable hover effect: True, False
image | put an image on the button, removes the text, must be class PhotoImage
state | tkinter.NORMAL (standard) or tkinter.DISABLED (not clickable, darker color)
CTkButton Methods:
```python
@ -167,6 +168,10 @@ CTkButton.configure_color(bg_color=new_bg_color,
fg_color=new_fg_color,
hover_color=new_hover_color,
text_color=new_text_color)
CTkButton.configure(state=tkinter.DISABLED)
CTkButton.configure(state=tkinter.NORMAL)
button_state = CTkButton.state
```
</details>

View File

@ -24,6 +24,7 @@ class CTkButton(tkinter.Frame):
text="CTkButton",
hover=True,
image=None,
state=tkinter.NORMAL,
*args, **kwargs):
super().__init__(*args, **kwargs)
@ -37,7 +38,7 @@ class CTkButton(tkinter.Frame):
AppearanceModeTracker.add(self.set_appearance_mode)
if fg_color == None:
if fg_color is None:
self.fg_color = self.bg_color
else:
self.fg_color = fg_color
@ -75,6 +76,7 @@ class CTkButton(tkinter.Frame):
self.text_font = text_font
self.function = command
self.state = state
self.hover = hover
self.image = image
@ -174,7 +176,16 @@ class CTkButton(tkinter.Frame):
for part in self.canvas_fg_parts:
if type(self.fg_color) == tuple and len(self.fg_color) == 2:
if self.state == tkinter.DISABLED:
self.canvas.itemconfig(part,
fill=CTkColorManager.darken_hex_color(self.fg_color[self.appearance_mode]), width=0)
else:
self.canvas.itemconfig(part, fill=self.fg_color[self.appearance_mode], width=0)
else:
if self.state == tkinter.DISABLED:
self.canvas.itemconfig(part,
fill=CTkColorManager.darken_hex_color(self.fg_color),
outline=self.fg_color, width=0)
else:
self.canvas.itemconfig(part, fill=self.fg_color, outline=self.fg_color, width=0)
@ -191,7 +202,6 @@ class CTkButton(tkinter.Frame):
font=self.text_font)
self.text_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
if self.hover is True:
self.text_label.bind("<Enter>", self.on_enter)
self.text_label.bind("<Leave>", self.on_leave)
@ -204,7 +214,13 @@ class CTkButton(tkinter.Frame):
self.text_label.configure(fg=self.text_color)
if type(self.fg_color) == tuple and len(self.fg_color) == 2:
if self.state == tkinter.DISABLED:
self.text_label.configure(bg=CTkColorManager.darken_hex_color(self.fg_color[self.appearance_mode]))
else:
self.text_label.configure(bg=self.fg_color[self.appearance_mode])
else:
if self.state == tkinter.DISABLED:
self.text_label.configure(bg=CTkColorManager.darken_hex_color(self.fg_color))
else:
self.text_label.configure(bg=self.fg_color)
@ -216,7 +232,6 @@ class CTkButton(tkinter.Frame):
image=self.image)
self.image_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
if self.hover is True:
self.image_label.bind("<Enter>", self.on_enter)
self.image_label.bind("<Leave>", self.on_leave)
@ -224,7 +239,13 @@ class CTkButton(tkinter.Frame):
self.image_label.bind("<Button-1>", self.clicked)
if type(self.fg_color) == tuple and len(self.fg_color) == 2:
if self.state == tkinter.DISABLED:
self.image_label.configure(bg=CTkColorManager.darken_hex_color(self.fg_color[self.appearance_mode]))
else:
self.image_label.configure(bg=self.fg_color[self.appearance_mode])
else:
if self.state == tkinter.DISABLED:
self.image_label.configure(bg=CTkColorManager.darken_hex_color(self.fg_color))
else:
self.image_label.configure(bg=self.fg_color)
@ -245,6 +266,35 @@ class CTkButton(tkinter.Frame):
self.draw()
def config(self, *args, **kwargs):
self.configure(*args, **kwargs)
def configure(self, *args, **kwargs):
if "text" in kwargs:
self.set_text(kwargs["text"])
del kwargs["text"]
if "state" in kwargs:
self.set_state(kwargs["state"])
del kwargs["state"]
super().configure(*args, **kwargs)
def set_state(self, state):
self.state = state
if self.state == tkinter.DISABLED:
self.hover = False
if sys.platform == "darwin" and self.function is not None:
self.configure(cursor="arrow")
elif self.state == tkinter.NORMAL:
self.hover = True
if sys.platform == "darwin" and self.function is not None:
self.configure(cursor="pointinghand")
self.draw()
def set_text(self, text):
self.text = text
if self.text_label is not None:
@ -260,6 +310,7 @@ class CTkButton(tkinter.Frame):
sys.stderr.write("ERROR (CTkButton): Cant change image because button has no image.")
def on_enter(self, event=0):
if self.hover is True:
for part in self.canvas_fg_parts:
if type(self.hover_color) == tuple and len(self.hover_color) == 2:
self.canvas.itemconfig(part, fill=self.hover_color[self.appearance_mode], width=0)
@ -279,6 +330,7 @@ class CTkButton(tkinter.Frame):
self.image_label.configure(bg=self.hover_color)
def on_leave(self, event=0):
if self.hover is True:
for part in self.canvas_fg_parts:
if type(self.fg_color) == tuple and len(self.fg_color) == 2:
self.canvas.itemconfig(part, fill=self.fg_color[self.appearance_mode], width=0)
@ -299,6 +351,7 @@ class CTkButton(tkinter.Frame):
def clicked(self, event=0):
if self.function is not None:
if self.state is not tkinter.DISABLED:
self.function()
self.on_leave()

View File

@ -11,6 +11,27 @@ class CTkColorManager:
FRAME = ("#D4D5D6", "#3F3F3F")
FRAME_2 = ("#BFBEC1", "#505050")
DARKEN_COLOR_FACTOR = 0.8 # used for generate color for disabled button
@staticmethod
def rgb2hex(rgb_color):
return "#{:02x}{:02x}{:02x}".format(round(rgb_color[0]), round(rgb_color[1]), round(rgb_color[2]))
@staticmethod
def hex2rgb(hex_color: str):
return tuple(int(hex_color.strip("#")[i:i+2], 16) for i in (0, 2, 4))
@staticmethod
def darken_hex_color(hex_color):
try:
rgb_color = CTkColorManager.hex2rgb(hex_color)
dark_rgb_color = (rgb_color[0] * CTkColorManager.DARKEN_COLOR_FACTOR,
rgb_color[1] * CTkColorManager.DARKEN_COLOR_FACTOR,
rgb_color[2] * CTkColorManager.DARKEN_COLOR_FACTOR)
return CTkColorManager.rgb2hex(dark_rgb_color)
except Exception as err:
return hex_color
@classmethod
def set_theme_color(cls, hex_color, hex_color_hover):
cls.MAIN = (hex_color, hex_color)

View File

@ -0,0 +1,35 @@
import tkinter
import customtkinter # <- import the CustomTkinter module
customtkinter.enable_macos_darkmode()
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
root_tk = tkinter.Tk() # create the Tk window like you normally do
root_tk.geometry("400x240")
root_tk.title("CustomTkinter Test")
def change_button_2_state():
if button_2.state == tkinter.NORMAL:
button_2.configure(state=tkinter.DISABLED)
elif button_2.state == tkinter.DISABLED:
button_2.configure(state=tkinter.NORMAL)
def button_2_click():
print("button_2 clicked")
frame_1 = customtkinter.CTkFrame(master=root_tk, width=300, height=200, corner_radius=15)
frame_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
button_1 = customtkinter.CTkButton(master=frame_1, text="Disable/Enable Button_2",
corner_radius=10, command=change_button_2_state, width=200)
button_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
button_2 = customtkinter.CTkButton(master=frame_1, text="Button_2",
corner_radius=10, command=button_2_click)
button_2.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
root_tk.mainloop()
customtkinter.disable_macos_darkmode()

View File

@ -29,6 +29,11 @@ progressbar_1.place(relx=0.5, rely=0.25, anchor=tkinter.CENTER)
button_1 = customtkinter.CTkButton(master=frame_1, corner_radius=10, command=button_function)
button_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
#button_1.configure(state="disabled")
for child in button_1.winfo_children():
child.configure(state='disable')
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_function)
slider_1.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)