added checkmark to CTkCheckbox

This commit is contained in:
TomSchimansky 2022-03-09 14:49:44 +01:00
parent 2ab5b7e19f
commit 9acd0c42df
8 changed files with 61 additions and 15 deletions

View File

@ -5,6 +5,7 @@
"button_hover": ["#1A89BF", "#1673A1"],
"button_border": ["gray40", "gray70"],
"checkbox_border": ["gray40", "gray60"],
"checkmark": ["white", "gray90"],
"entry": ["white", "gray24"],
"entry_border": ["gray70", "gray32"],
"entry_placeholder_text": ["gray52", "gray62"],

View File

@ -5,6 +5,7 @@
"button_hover": ["#A4BDE6", "#748BB3"],
"button_border": ["gray40", "gray70"],
"checkbox_border": ["gray40", "gray60"],
"checkmark": ["white", "gray90"],
"entry": ["white", "gray24"],
"entry_border": ["gray70", "gray32"],
"entry_placeholder_text": ["gray52", "gray62"],

View File

@ -6,6 +6,7 @@
"button_hover": ["#0E9670", "#0D8A66"],
"button_border": ["gray40", "gray70"],
"checkbox_border": ["gray40", "gray60"],
"checkmark": ["white", "gray90"],
"entry": ["white", "gray24"],
"entry_border": ["gray70", "gray32"],
"entry_placeholder_text": ["gray52", "gray62"],

View File

@ -19,6 +19,7 @@ class CTkCheckBox(tkinter.Frame):
hover_color="default_theme",
border_color="default_theme",
border_width="default_theme",
checkmark_color="default_theme",
width=24,
height=24,
corner_radius="default_theme",
@ -63,6 +64,7 @@ class CTkCheckBox(tkinter.Frame):
self.fg_color = CTkThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
self.hover_color = CTkThemeManager.theme["color"]["button"] if hover_color == "default_theme" else hover_color
self.border_color = CTkThemeManager.theme["color"]["checkbox_border"] if border_color == "default_theme" else border_color
self.checkmark_color = CTkThemeManager.theme["color"]["checkmark"] if checkmark_color == "default_theme" else checkmark_color
self.width = width
self.height = height
@ -147,21 +149,33 @@ class CTkCheckBox(tkinter.Frame):
def draw(self):
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.width, self.height, self.corner_radius, self.border_width)
if self.check_state is True:
self.draw_engine.draw_checkmark(self.width, self.height, self.height * 0.58)
else:
self.canvas.delete("checkmark")
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
if self.check_state is False:
self.canvas.itemconfig("inner_parts",
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
else:
if self.check_state is True:
self.canvas.itemconfig("inner_parts",
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
self.canvas.itemconfig("border_parts",
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
self.canvas.itemconfig("border_parts",
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
if "create_line" in self.canvas.gettags("checkmark"):
self.canvas.itemconfig("checkmark", fill=CTkThemeManager.single_color(self.checkmark_color, self.appearance_mode))
else:
self.canvas.itemconfig("checkmark", fill=CTkThemeManager.single_color(self.checkmark_color, self.appearance_mode))
else:
self.canvas.itemconfig("inner_parts",
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.canvas.itemconfig("border_parts",
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
if self.text_label is None:
self.text_label = tkinter.Label(master=self,
@ -299,14 +313,10 @@ class CTkCheckBox(tkinter.Frame):
if self.state == tkinter.NORMAL:
if self.check_state is True:
self.check_state = False
self.canvas.itemconfig("inner_parts",
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.draw()
else:
self.check_state = True
self.canvas.itemconfig("inner_parts",
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
self.draw()
if self.function is not None:
self.function()

View File

@ -15,6 +15,7 @@ class CTkDrawEngine:
- draw_rounded_rect_with_border()
- draw_rounded_progress_bar_with_border()
- draw_rounded_slider_with_border_and_button()
- draw_checkmark()
"""
@ -627,3 +628,33 @@ class CTkDrawEngine:
self._canvas.tag_raise("slider_parts")
return requires_recoloring
def draw_checkmark(self, width: int, height: int, size: Union[int, float]) -> bool:
""" Draws a rounded rectangle with a corner_radius and border_width on the canvas. The border elements have a 'border_parts' tag,
the main foreground elements have an 'inner_parts' tag to color the elements accordingly.
returns bool if recoloring is necessary """
size = round(size)
requires_recoloring = False
if self._rendering_method == "polygon_shapes" or self._rendering_method == "circle_shapes":
x, y, radius = width / 2, height / 2, size / 2.2
if not self._canvas.find_withtag("checkmark"):
self._canvas.create_line(0, 0, 0, 0, tags=("checkmark", "create_line"), width=round(height / 8), joinstyle=tkinter.MITER, capstyle=tkinter.ROUND)
self._canvas.tag_raise("checkmark")
requires_recoloring = True
self._canvas.coords("checkmark",
x + radius, y - radius,
x - radius / 4, y + radius,
x - radius, y + radius / 4)
elif self._rendering_method == "font_shapes":
if not self._canvas.find_withtag("checkmark"):
self._canvas.create_text(0, 0, text="Z", font=("CustomTkinter_shapes_font", -size), tags=("checkmark", "create_text"), anchor=tkinter.CENTER)
self._canvas.tag_raise("checkmark")
requires_recoloring = True
self._canvas.coords("checkmark", round(width / 2), round(height / 2))
return requires_recoloring

View File

@ -4,7 +4,9 @@ import customtkinter
import sys
customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.CTkSettings.preferred_drawing_method = "circle_shapes"
class App(customtkinter.CTk):