2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
from .customtkinter_tk import CTk
|
2021-03-04 20:27:46 +03:00
|
|
|
from .customtkinter_frame import CTkFrame
|
|
|
|
from .appearance_mode_tracker import AppearanceModeTracker
|
2022-02-23 00:38:40 +03:00
|
|
|
from .customtkinter_theme_manager import CTkThemeManager
|
2022-02-22 16:36:36 +03:00
|
|
|
from .customtkinter_canvas import CTkCanvas
|
2022-02-23 00:38:40 +03:00
|
|
|
from .customtkinter_settings import CTkSettings
|
2022-03-01 18:12:55 +03:00
|
|
|
from .customtkinter_draw_engine import CTkDrawEngine
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkButton(tkinter.Frame):
|
|
|
|
""" tkinter custom button with border, rounded corners and hover effect """
|
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
def __init__(self, *args,
|
2021-03-04 20:27:46 +03:00
|
|
|
bg_color=None,
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
|
|
|
hover_color="default_theme",
|
|
|
|
border_color="default_theme",
|
2022-02-28 02:35:52 +03:00
|
|
|
border_width="default_theme",
|
2021-03-04 20:27:46 +03:00
|
|
|
command=None,
|
2022-01-16 18:54:21 +03:00
|
|
|
textvariable=None,
|
2021-03-04 20:27:46 +03:00
|
|
|
width=120,
|
2022-01-07 22:54:45 +03:00
|
|
|
height=30,
|
2022-02-28 02:35:52 +03:00
|
|
|
corner_radius="default_theme",
|
2022-02-23 03:47:34 +03:00
|
|
|
text_font="default_theme",
|
2022-02-23 00:38:40 +03:00
|
|
|
text_color="default_theme",
|
2021-03-04 20:27:46 +03:00
|
|
|
text="CTkButton",
|
|
|
|
hover=True,
|
2021-03-08 20:17:36 +03:00
|
|
|
image=None,
|
2021-11-06 02:35:07 +03:00
|
|
|
compound=tkinter.LEFT,
|
2021-07-23 01:44:13 +03:00
|
|
|
state=tkinter.NORMAL,
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs):
|
2021-03-04 20:27:46 +03:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
# overwrite configure methods of master when master is tkinter widget, so that bg changes get applied on child CTk widget too
|
|
|
|
if isinstance(self.master, (tkinter.Tk, tkinter.Frame)) and not isinstance(self.master, (CTk, CTkFrame)):
|
|
|
|
master_old_configure = self.master.config
|
|
|
|
|
|
|
|
def new_configure(*args, **kwargs):
|
|
|
|
if "bg" in kwargs:
|
|
|
|
self.configure(bg_color=kwargs["bg"])
|
|
|
|
elif "background" in kwargs:
|
|
|
|
self.configure(bg_color=kwargs["background"])
|
|
|
|
|
|
|
|
# args[0] is dict when attribute gets changed by widget[<attribute>] syntax
|
|
|
|
elif len(args) > 0 and type(args[0]) == dict:
|
|
|
|
if "bg" in args[0]:
|
|
|
|
self.configure(bg_color=args[0]["bg"])
|
|
|
|
elif "background" in args[0]:
|
|
|
|
self.configure(bg_color=args[0]["background"])
|
|
|
|
master_old_configure(*args, **kwargs)
|
|
|
|
|
|
|
|
self.master.config = new_configure
|
|
|
|
self.master.configure = new_configure
|
|
|
|
|
|
|
|
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
2021-11-06 02:35:07 +03:00
|
|
|
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
2021-09-13 15:27:29 +03:00
|
|
|
|
2021-11-05 14:21:55 +03:00
|
|
|
self.configure_basic_grid()
|
2021-11-04 18:20:08 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
# color variables
|
2021-11-07 00:06:27 +03:00
|
|
|
self.bg_color = self.detect_color_of_master() if bg_color is None else bg_color
|
2022-02-28 02:35:52 +03:00
|
|
|
self.fg_color = CTkThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.hover_color = CTkThemeManager.theme["color"]["button_hover"] if hover_color == "default_theme" else hover_color
|
2022-03-02 02:53:58 +03:00
|
|
|
self.border_color = CTkThemeManager.theme["color"]["button_border"] if border_color == "default_theme" else border_color
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
# shape and size
|
2021-03-04 20:27:46 +03:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2022-01-18 01:23:47 +03:00
|
|
|
self.configure(width=self.width, height=self.height)
|
2022-02-28 02:35:52 +03:00
|
|
|
self.corner_radius = CTkThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
|
|
|
self.border_width = CTkThemeManager.theme["shape"]["button_border_width"] if border_width == "default_theme" else border_width
|
2021-11-10 02:03:08 +03:00
|
|
|
|
|
|
|
if self.corner_radius * 2 > self.height:
|
2021-11-04 18:20:08 +03:00
|
|
|
self.corner_radius = self.height / 2
|
2021-11-10 02:03:08 +03:00
|
|
|
elif self.corner_radius * 2 > self.width:
|
2021-11-04 18:20:08 +03:00
|
|
|
self.corner_radius = self.width / 2
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
if self.corner_radius >= self.border_width:
|
|
|
|
self.inner_corner_radius = self.corner_radius - self.border_width
|
|
|
|
else:
|
|
|
|
self.inner_corner_radius = 0
|
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
# text and font and image
|
|
|
|
self.image = image
|
|
|
|
self.image_label = None
|
2021-03-04 20:27:46 +03:00
|
|
|
self.text = text
|
2022-01-18 01:23:47 +03:00
|
|
|
self.text_label = None
|
2022-02-28 02:35:52 +03:00
|
|
|
self.text_color = CTkThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
|
|
|
self.text_font = (CTkThemeManager.theme["text"]["font"], CTkThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
# callback and hover functionality
|
2021-03-04 20:27:46 +03:00
|
|
|
self.function = command
|
2022-01-16 18:54:21 +03:00
|
|
|
self.textvariable = textvariable
|
2021-07-23 01:44:13 +03:00
|
|
|
self.state = state
|
2021-03-04 20:27:46 +03:00
|
|
|
self.hover = hover
|
2021-11-06 02:35:07 +03:00
|
|
|
self.compound = compound
|
2022-01-18 01:23:47 +03:00
|
|
|
self.click_animation_running = False
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-23 01:50:03 +03:00
|
|
|
if sys.platform == "darwin" and self.function is not None and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.configure(cursor="pointinghand")
|
|
|
|
elif sys.platform.startswith("win") and self.function is not None and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.configure(cursor="hand2")
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-22 16:36:36 +03:00
|
|
|
self.canvas = CTkCanvas(master=self,
|
2022-02-23 01:50:03 +03:00
|
|
|
highlightthickness=0,
|
|
|
|
width=self.width,
|
|
|
|
height=self.height)
|
2022-02-08 22:59:50 +03:00
|
|
|
self.canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew")
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-03-01 18:12:55 +03:00
|
|
|
self.draw_engine = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method)
|
2022-02-24 02:19:40 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
# event bindings
|
2021-03-04 20:27:46 +03:00
|
|
|
if self.hover is True:
|
|
|
|
self.canvas.bind("<Enter>", self.on_enter)
|
|
|
|
self.canvas.bind("<Leave>", self.on_leave)
|
|
|
|
|
|
|
|
self.canvas.bind("<Button-1>", self.clicked)
|
|
|
|
self.canvas.bind("<Button-1>", self.clicked)
|
|
|
|
|
2021-11-04 18:20:08 +03:00
|
|
|
# Each time an item is resized due to pack position mode, the binding Configure is called on the widget
|
2021-11-05 14:21:55 +03:00
|
|
|
self.bind('<Configure>', self.update_dimensions)
|
2022-01-18 01:23:47 +03:00
|
|
|
|
|
|
|
self.draw() # initial draw
|
2021-11-04 18:20:08 +03:00
|
|
|
|
2022-01-02 00:16:06 +03:00
|
|
|
def destroy(self):
|
|
|
|
AppearanceModeTracker.remove(self.set_appearance_mode)
|
|
|
|
super().destroy()
|
|
|
|
|
2021-11-05 14:21:55 +03:00
|
|
|
def configure_basic_grid(self):
|
2021-11-06 02:35:07 +03:00
|
|
|
# Configuration of a basic grid (2x2) in which all elements of CTkButtons are centered on one row and one column
|
2021-11-04 18:20:08 +03:00
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
2021-11-06 02:35:07 +03:00
|
|
|
self.grid_rowconfigure(1, weight=1)
|
|
|
|
self.grid_columnconfigure(1, weight=1)
|
2021-11-04 18:20:08 +03:00
|
|
|
|
2021-11-05 14:21:55 +03:00
|
|
|
def update_dimensions(self, event):
|
2021-11-07 02:01:17 +03:00
|
|
|
# only redraw if dimensions changed (for performance)
|
|
|
|
if self.width != event.width or self.height != event.height:
|
|
|
|
self.width = event.width
|
|
|
|
self.height = event.height
|
2022-01-18 01:23:47 +03:00
|
|
|
|
2022-01-23 19:06:34 +03:00
|
|
|
# self.canvas.config(width=self.width, height=self.height)
|
2021-11-07 17:12:09 +03:00
|
|
|
self.draw(no_color_updates=True) # fast drawing without color changes
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
def detect_color_of_master(self):
|
|
|
|
if isinstance(self.master, CTkFrame):
|
|
|
|
return self.master.fg_color
|
|
|
|
else:
|
|
|
|
return self.master.cget("bg")
|
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
def draw(self, no_color_updates=False):
|
2022-02-24 02:19:40 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.width, self.height, self.corner_radius, self.border_width)
|
|
|
|
|
|
|
|
if no_color_updates is False or requires_recoloring:
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2022-02-24 02:19:40 +03:00
|
|
|
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
2021-11-07 17:12:09 +03:00
|
|
|
|
2021-11-10 02:03:08 +03:00
|
|
|
# set color for the button border parts (outline)
|
2021-11-10 14:15:39 +03:00
|
|
|
self.canvas.itemconfig("border_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
|
2021-11-10 02:03:08 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
# set color for inner button parts (depends on button state)
|
2021-11-06 02:35:07 +03:00
|
|
|
if self.state == tkinter.DISABLED:
|
2022-01-07 22:54:45 +03:00
|
|
|
if self.fg_color is None:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.bg_color, self.appearance_mode)),
|
|
|
|
fill=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.bg_color, self.appearance_mode)))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-24 02:19:40 +03:00
|
|
|
outline=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.fg_color, self.appearance_mode)),
|
|
|
|
fill=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.fg_color, self.appearance_mode)))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-10 02:03:08 +03:00
|
|
|
else:
|
2022-01-07 22:54:45 +03:00
|
|
|
if self.fg_color is None:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
# create text label if text given
|
|
|
|
if self.text is not None and self.text != "":
|
|
|
|
|
|
|
|
if self.text_label is None:
|
2022-01-16 18:54:21 +03:00
|
|
|
self.text_label = tkinter.Label(master=self, font=self.text_font, textvariable=self.textvariable)
|
2021-11-07 02:01:17 +03:00
|
|
|
|
|
|
|
self.text_label.bind("<Enter>", self.on_enter)
|
|
|
|
self.text_label.bind("<Leave>", self.on_leave)
|
|
|
|
self.text_label.bind("<Button-1>", self.clicked)
|
|
|
|
self.text_label.bind("<Button-1>", self.clicked)
|
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
if no_color_updates is False:
|
2021-11-07 02:01:17 +03:00
|
|
|
# set text_label fg color (text color)
|
2022-02-23 00:38:40 +03:00
|
|
|
self.text_label.configure(fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
|
|
|
# set text_label bg color (label color)
|
|
|
|
if self.state == tkinter.DISABLED:
|
2022-01-07 22:54:45 +03:00
|
|
|
if self.fg_color is None:
|
2022-02-24 02:19:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.bg_color, self.appearance_mode)))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
2022-02-24 02:19:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.fg_color, self.appearance_mode)))
|
2021-11-07 02:01:17 +03:00
|
|
|
else:
|
2022-01-07 22:54:45 +03:00
|
|
|
if self.fg_color is None:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
self.text_label.configure(text=self.text) # set text
|
|
|
|
|
|
|
|
else:
|
|
|
|
# delete text_label if no text given
|
|
|
|
if self.text_label is not None:
|
|
|
|
self.text_label.destroy()
|
|
|
|
self.text_label = None
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
# create image label if image given
|
|
|
|
if self.image is not None:
|
|
|
|
|
|
|
|
if self.image_label is None:
|
|
|
|
self.image_label = tkinter.Label(master=self)
|
2021-11-07 02:01:17 +03:00
|
|
|
|
|
|
|
self.image_label.bind("<Enter>", self.on_enter)
|
|
|
|
self.image_label.bind("<Leave>", self.on_leave)
|
|
|
|
self.image_label.bind("<Button-1>", self.clicked)
|
|
|
|
self.image_label.bind("<Button-1>", self.clicked)
|
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
if no_color_updates is False:
|
2021-11-07 02:01:17 +03:00
|
|
|
# set image_label bg color (background color of label)
|
|
|
|
if self.state == tkinter.DISABLED:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.image_label.configure(bg=CTkThemeManager.darken_hex_color(CTkThemeManager.single_color(self.fg_color, self.appearance_mode)))
|
2021-11-07 02:01:17 +03:00
|
|
|
else:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.image_label.configure(bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
2021-11-07 02:01:17 +03:00
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
self.image_label.configure(image=self.image) # set image
|
|
|
|
|
|
|
|
else:
|
|
|
|
# delete text_label if no text given
|
|
|
|
if self.image_label is not None:
|
|
|
|
self.image_label.destroy()
|
|
|
|
self.image_label = None
|
|
|
|
|
|
|
|
# create grid layout with just an image given
|
|
|
|
if self.image_label is not None and self.text_label is None:
|
|
|
|
self.image_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="")
|
|
|
|
|
|
|
|
# create grid layout with just text given
|
|
|
|
if self.image_label is None and self.text_label is not None:
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.corner_radius, pady=self.border_width, rowspan=2, columnspan=2, sticky="")
|
|
|
|
|
|
|
|
# create grid layout of image and text label in 2x2 grid system with given compound
|
|
|
|
if self.image_label is not None and self.text_label is not None:
|
|
|
|
if self.compound == tkinter.LEFT or self.compound == "left":
|
|
|
|
self.image_label.grid(row=0, column=0, padx=self.corner_radius, sticky="e", rowspan=2, columnspan=1)
|
|
|
|
self.text_label.grid(row=0, column=1, padx=self.corner_radius, sticky="w", rowspan=2, columnspan=1, pady=self.border_width)
|
|
|
|
elif self.compound == tkinter.TOP or self.compound == "top":
|
|
|
|
self.image_label.grid(row=0, column=0, padx=self.corner_radius, sticky="s", columnspan=2, rowspan=1)
|
|
|
|
self.text_label.grid(row=1, column=0, padx=self.corner_radius, sticky="n", columnspan=2, rowspan=1, pady=self.border_width)
|
|
|
|
elif self.compound == tkinter.RIGHT or self.compound == "right":
|
|
|
|
self.image_label.grid(row=0, column=1, padx=self.corner_radius, sticky="w", rowspan=2, columnspan=1)
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.corner_radius, sticky="e", rowspan=2, columnspan=1, pady=self.border_width)
|
|
|
|
elif self.compound == tkinter.BOTTOM or self.compound == "bottom":
|
|
|
|
self.image_label.grid(row=1, column=0, padx=self.corner_radius, sticky="n", columnspan=2, rowspan=1)
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.corner_radius, sticky="s", columnspan=2, rowspan=1, pady=self.border_width)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
def config(self, *args, **kwargs):
|
|
|
|
self.configure(*args, **kwargs)
|
|
|
|
|
|
|
|
def configure(self, *args, **kwargs):
|
2021-11-07 00:06:27 +03:00
|
|
|
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
if "text" in kwargs:
|
|
|
|
self.set_text(kwargs["text"])
|
|
|
|
del kwargs["text"]
|
|
|
|
|
|
|
|
if "state" in kwargs:
|
|
|
|
self.set_state(kwargs["state"])
|
|
|
|
del kwargs["state"]
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "image" in kwargs:
|
|
|
|
self.set_image(kwargs["image"])
|
|
|
|
del kwargs["image"]
|
|
|
|
|
2021-11-07 17:12:09 +03:00
|
|
|
if "compound" in kwargs:
|
|
|
|
self.compound = kwargs["compound"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["compound"]
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["fg_color"]
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if "border_color" in kwargs:
|
|
|
|
self.border_color = kwargs["border_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["border_color"]
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "bg_color" in kwargs:
|
|
|
|
if kwargs["bg_color"] is None:
|
|
|
|
self.bg_color = self.detect_color_of_master()
|
|
|
|
else:
|
|
|
|
self.bg_color = kwargs["bg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["bg_color"]
|
|
|
|
|
|
|
|
if "hover_color" in kwargs:
|
|
|
|
self.hover_color = kwargs["hover_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["hover_color"]
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
|
|
|
self.text_color = kwargs["text_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["text_color"]
|
|
|
|
|
2022-01-08 15:13:10 +03:00
|
|
|
if "command" in kwargs:
|
|
|
|
self.function = kwargs["command"]
|
|
|
|
del kwargs["command"]
|
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
if "textvariable" in kwargs:
|
|
|
|
self.textvariable = kwargs["textvariable"]
|
|
|
|
if self.text_label is not None:
|
|
|
|
self.text_label.configure(textvariable=self.textvariable)
|
|
|
|
del kwargs["textvariable"]
|
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
super().configure(*args, **kwargs)
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if require_redraw:
|
|
|
|
self.draw()
|
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
def set_state(self, state):
|
|
|
|
self.state = state
|
|
|
|
|
|
|
|
if self.state == tkinter.DISABLED:
|
|
|
|
self.hover = False
|
2022-02-23 01:50:03 +03:00
|
|
|
if sys.platform == "darwin" and self.function is not None and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.configure(cursor="arrow")
|
|
|
|
elif sys.platform.startswith("win") and self.function is not None and CTkSettings.hand_cursor_enabled:
|
2021-07-23 01:44:13 +03:00
|
|
|
self.configure(cursor="arrow")
|
|
|
|
|
|
|
|
elif self.state == tkinter.NORMAL:
|
|
|
|
self.hover = True
|
2022-02-23 01:50:03 +03:00
|
|
|
if sys.platform == "darwin" and self.function is not None and CTkSettings.hand_cursor_enabled:
|
2021-07-23 01:44:13 +03:00
|
|
|
self.configure(cursor="pointinghand")
|
2022-02-23 01:50:03 +03:00
|
|
|
elif sys.platform.startswith("win") and self.function is not None and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.configure(cursor="hand2")
|
2021-07-23 01:44:13 +03:00
|
|
|
|
|
|
|
self.draw()
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def set_text(self, text):
|
2021-03-08 20:17:36 +03:00
|
|
|
self.text = text
|
2021-11-07 17:12:09 +03:00
|
|
|
self.draw()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
def set_image(self, image):
|
2021-11-07 17:12:09 +03:00
|
|
|
self.image = image
|
|
|
|
self.draw()
|
2021-03-08 20:28:40 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def on_enter(self, event=0):
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.hover is True:
|
2022-01-08 02:29:53 +03:00
|
|
|
if self.hover_color is None:
|
|
|
|
inner_parts_color = self.fg_color
|
|
|
|
else:
|
|
|
|
inner_parts_color = self.hover_color
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set color of inner button parts to hover color
|
2021-11-10 14:15:39 +03:00
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set text_label bg color to button hover color
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.text_label is not None:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set image_label bg color to button hover color
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.image_label is not None:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.image_label.configure(bg=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-03-08 20:17:36 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def on_leave(self, event=0):
|
2022-01-18 01:23:47 +03:00
|
|
|
self.click_animation_running = False
|
|
|
|
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.hover is True:
|
2022-01-07 22:54:45 +03:00
|
|
|
if self.fg_color is None:
|
|
|
|
inner_parts_color = self.bg_color
|
|
|
|
else:
|
|
|
|
inner_parts_color = self.fg_color
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set color of inner button parts
|
2021-11-10 14:15:39 +03:00
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-02-23 00:38:40 +03:00
|
|
|
outline=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set text_label bg color (label color)
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.text_label is not None:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-07-23 01:44:13 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
# set image_label bg color (image bg color)
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.image_label is not None:
|
2022-02-23 00:38:40 +03:00
|
|
|
self.image_label.configure(bg=CTkThemeManager.single_color(inner_parts_color, self.appearance_mode))
|
2021-03-08 20:17:36 +03:00
|
|
|
|
2022-01-18 01:23:47 +03:00
|
|
|
def click_animation(self):
|
|
|
|
if self.click_animation_running:
|
|
|
|
self.on_enter()
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def clicked(self, event=0):
|
|
|
|
if self.function is not None:
|
2021-07-23 01:44:13 +03:00
|
|
|
if self.state is not tkinter.DISABLED:
|
2022-01-18 01:23:47 +03:00
|
|
|
|
|
|
|
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
2021-07-23 01:44:13 +03:00
|
|
|
self.on_leave()
|
2022-01-18 01:23:47 +03:00
|
|
|
self.click_animation_running = True
|
|
|
|
self.after(100, self.click_animation)
|
|
|
|
|
2022-01-02 14:14:46 +03:00
|
|
|
self.function()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
def set_appearance_mode(self, mode_string):
|
|
|
|
if mode_string.lower() == "dark":
|
|
|
|
self.appearance_mode = 1
|
|
|
|
elif mode_string.lower() == "light":
|
|
|
|
self.appearance_mode = 0
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if isinstance(self.master, (CTkFrame, CTk)):
|
|
|
|
self.bg_color = self.master.fg_color
|
|
|
|
else:
|
|
|
|
self.bg_color = self.master.cget("bg")
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
self.draw()
|