Merge branch 'develop'

# Conflicts:
#	customtkinter/__init__.py
#	pyproject.toml
#	setup.cfg
This commit is contained in:
Tom Schimansky 2022-06-23 22:22:11 +02:00
commit d4d0cf1188
14 changed files with 531 additions and 68 deletions

View File

@ -1,4 +1,4 @@
__version__ = "4.4.1"
__version__ = "4.5.0"
import os
import sys
@ -14,7 +14,15 @@ from .font_manager import FontManager
from .draw_engine import DrawEngine
AppearanceModeTracker.init_appearance_mode()
ThemeManager.load_theme("blue") # load default theme
# load default blue theme
try:
ThemeManager.load_theme("blue")
except FileNotFoundError as err:
raise FileNotFoundError(f"{err}\n\nThe .json theme file for CustomTkinter could not be found.\n" +
f"If packaging with pyinstaller was used, have a look at the wiki:\n" +
f"https://github.com/TomSchimansky/CustomTkinter/wiki/Packaging#windows-pyinstaller-auto-py-to-exe")
FontManager.init_font_manager()
# determine draw method based on current platform
@ -53,6 +61,7 @@ from .widgets.ctk_canvas import CTkCanvas
from .widgets.ctk_switch import CTkSwitch
from .widgets.ctk_optionmenu import CTkOptionMenu
from .widgets.ctk_combobox import CTkComboBox
from .widgets.ctk_scrollbar import CTkScrollbar
# import windows
from .windows.ctk_tk import CTk

View File

@ -11,7 +11,7 @@
"entry_placeholder_text": ["gray52", "gray62"],
"frame_border": ["#979DA2", "#1F2122"],
"frame_low": ["#D1D5D8", "#2A2D2E"],
"frame_high": ["#D7D8D9", "#343638"],
"frame_high": ["#C0C2C5", "#343638"],
"label": [null, null],
"text": ["gray10", "#DCE4EE"],
"text_disabled": ["gray60", "#777B80"],
@ -33,7 +33,9 @@
"combobox_button_hover": ["#6E7174", "#7A848D"],
"dropdown_color": ["gray90", "gray20"],
"dropdown_hover": ["gray75", "gray28"],
"dropdown_text": ["gray10", "#DCE4EE"]
"dropdown_text": ["gray10", "#DCE4EE"],
"scrollbar_button": ["gray55", "gray41"],
"scrollbar_button_hover": ["gray40", "gray53"]
},
"text": {
"macOS": {
@ -70,6 +72,8 @@
"switch_border_width": 3,
"switch_corner_radius": 1000,
"switch_button_corner_radius": 1000,
"switch_button_length": 0
"switch_button_length": 0,
"scrollbar_corner_radius": 1000,
"scrollbar_border_spacing": 4
}
}

View File

@ -33,7 +33,9 @@
"combobox_button_hover": ["#6E7174", "#7A848D"],
"dropdown_color": ["gray90", "gray20"],
"dropdown_hover": ["gray75", "gray28"],
"dropdown_text": ["gray10", "#DCE4EE"]
"dropdown_text": ["gray10", "#DCE4EE"],
"scrollbar_button": ["gray55", "gray41"],
"scrollbar_button_hover": ["gray40", "gray53"]
},
"text": {
"macOS": {

View File

@ -33,7 +33,9 @@
"combobox_button_hover": ["#6E7174", "#7A848D"],
"dropdown_color": ["gray90", "gray20"],
"dropdown_hover": ["gray75", "gray28"],
"dropdown_text": ["gray10", "#DCE4EE"]
"dropdown_text": ["gray10", "#DCE4EE"],
"scrollbar_button": ["gray55", "gray41"],
"scrollbar_button_hover": ["gray40", "gray53"]
},
"text": {
"macOS": {

View File

@ -33,7 +33,9 @@
"combobox_button_hover": ["#6E7174", "#7A848D"],
"dropdown_color": ["gray90", "gray20"],
"dropdown_hover": ["gray75", "gray28"],
"dropdown_text": ["gray10", "#DCE4EE"]
"dropdown_text": ["gray10", "#DCE4EE"],
"scrollbar_button": ["gray55", "gray41"],
"scrollbar_button_hover": ["gray40", "gray53"]
},
"text": {
"macOS": {

View File

@ -20,6 +20,7 @@ class DrawEngine:
- draw_rounded_rect_with_border_vertical_split()
- draw_rounded_progress_bar_with_border()
- draw_rounded_slider_with_border_and_button()
- draw_rounded_scrollbar()
- draw_checkmark()
- draw_dropdown_arrow()
@ -38,7 +39,7 @@ class DrawEngine:
else:
return round(user_corner_radius)
# optimize forx drawing with antialiased font shapes
# optimize for drawing with antialiased font shapes
elif self.preferred_drawing_method == "font_shapes":
return round(user_corner_radius)
@ -397,8 +398,8 @@ class DrawEngine:
if not self._canvas.find_withtag("border_parts"):
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_left_1", "border_parts_left", "border_parts", "left_parts"))
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_right_1", "border_parts_right", "border_parts", "right_parts"))
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_left_1", "border_parts_left", "border_parts", "left_parts"))
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_right_1", "border_parts_right", "border_parts", "right_parts"))
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_left_1", "border_parts_left", "border_parts", "left_parts"), width=0)
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_right_1", "border_parts_right", "border_parts", "right_parts"), width=0)
requires_recoloring = True
self._canvas.coords("border_line_left_1",
@ -490,22 +491,27 @@ class DrawEngine:
# create canvas border corner parts if not already created, but only if needed, and delete if not needed
if not self._canvas.find_withtag("border_oval_1_a") and "border_oval_1" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_1_a", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_1_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_1_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("border_oval_1_a") and "border_oval_1" in exclude_parts:
self._canvas.delete("border_oval_1_a", "border_oval_1_b")
if not self._canvas.find_withtag("border_oval_2_a") and width > 2 * corner_radius and "border_oval_2" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"),
anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_2_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"),
anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("border_oval_2_a") and (not width > 2 * corner_radius or "border_oval_2" in exclude_parts):
self._canvas.delete("border_oval_2_a", "border_oval_2_b")
if not self._canvas.find_withtag("border_oval_3_a") and height > 2 * corner_radius \
and width > 2 * corner_radius and "border_oval_3" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_a", "border_corner_part", "border_parts_right", "border_parts", "right_parts"),
anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_3_b", "border_corner_part", "border_parts_right", "border_parts", "right_parts"),
anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("border_oval_3_a") and (not (height > 2 * corner_radius
and width > 2 * corner_radius) or "border_oval_3" in exclude_parts):
@ -513,7 +519,8 @@ class DrawEngine:
if not self._canvas.find_withtag("border_oval_4_a") and height > 2 * corner_radius and "border_oval_4" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_4_a", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_4_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("border_oval_4_b", "border_corner_part", "border_parts_left", "border_parts", "left_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("border_oval_4_a") and (not height > 2 * corner_radius or "border_oval_4" in exclude_parts):
self._canvas.delete("border_oval_4_a", "border_oval_4_b")
@ -554,14 +561,16 @@ class DrawEngine:
# create canvas border corner parts if not already created, but only if they're needed and delete if not needed
if not self._canvas.find_withtag("inner_oval_1_a") and "inner_oval_1" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_1_a", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_1_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_1_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("inner_oval_1_a") and "inner_oval_1" in exclude_parts:
self._canvas.delete("inner_oval_1_a", "inner_oval_1_b")
if not self._canvas.find_withtag("inner_oval_2_a") and width - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_2" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_a", "inner_corner_part","inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_a", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_2_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("inner_oval_2_a") and (not width - (2 * border_width) > 2 * inner_corner_radius or "inner_oval_2" in exclude_parts):
self._canvas.delete("inner_oval_2_a", "inner_oval_2_b")
@ -569,7 +578,8 @@ class DrawEngine:
if not self._canvas.find_withtag("inner_oval_3_a") and height - (2 * border_width) > 2 * inner_corner_radius \
and width - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_3" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_3_a", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_3_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_3_b", "inner_corner_part", "inner_parts_right", "inner_parts", "right_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("inner_oval_3_a") and (not (height - (2 * border_width) > 2 * inner_corner_radius
and width - (2 * border_width) > 2 * inner_corner_radius) or "inner_oval_3" in exclude_parts):
@ -577,7 +587,8 @@ class DrawEngine:
if not self._canvas.find_withtag("inner_oval_4_a") and height - (2 * border_width) > 2 * inner_corner_radius and "inner_oval_4" not in exclude_parts:
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_4_a", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_4_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER, angle=180)
self._canvas.create_aa_circle(0, 0, 0, tags=("inner_oval_4_b", "inner_corner_part", "inner_parts_left", "inner_parts", "left_parts"), anchor=tkinter.CENTER,
angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("inner_oval_4_a") and (not height - (2 * border_width) > 2 * inner_corner_radius or "inner_oval_4" in exclude_parts):
self._canvas.delete("inner_oval_4_a", "inner_oval_4_b")
@ -959,6 +970,146 @@ class DrawEngine:
return requires_recoloring
def draw_rounded_scrollbar(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
border_spacing: Union[float, int], start_value: float, end_value: float, orientation: str) -> bool:
width = math.floor(width / 2) * 2 # round _current_width and _current_height and restrict them to even values only
height = math.floor(height / 2) * 2
if corner_radius > width / 2 or corner_radius > height / 2: # restrict corner_radius if it's too larger
corner_radius = min(width / 2, height / 2)
border_spacing = round(border_spacing)
corner_radius = self.__calc_optimal_corner_radius(corner_radius) # optimize corner_radius for different drawing methods (different rounding)
if corner_radius >= border_spacing:
inner_corner_radius = corner_radius - border_spacing
else:
inner_corner_radius = 0
if self.preferred_drawing_method == "polygon_shapes" or self.preferred_drawing_method == "circle_shapes":
return self.__draw_rounded_scrollbar_polygon_shapes(width, height, corner_radius, inner_corner_radius,
start_value, end_value, orientation)
elif self.preferred_drawing_method == "font_shapes":
return self.__draw_rounded_scrollbar_font_shapes(width, height, corner_radius, inner_corner_radius,
start_value, end_value, orientation)
def __draw_rounded_scrollbar_polygon_shapes(self, width: int, height: int, corner_radius: int, inner_corner_radius: int,
start_value: float, end_value: float, orientation: str) -> bool:
requires_recoloring = False
if not self._canvas.find_withtag("border_parts"):
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_1", "border_parts"), width=0)
requires_recoloring = True
self._canvas.coords("border_rectangle_1", 0, 0, width, height)
if not self._canvas.find_withtag("scrollbar_parts"):
self._canvas.create_polygon((0, 0, 0, 0), tags=("scrollbar_polygon_1", "scrollbar_parts"), joinstyle=tkinter.ROUND)
self._canvas.tag_raise("scrollbar_parts", "border_parts")
requires_recoloring = True
if orientation == "vertical":
self._canvas.coords("scrollbar_polygon_1",
corner_radius, corner_radius + (height - 2 * corner_radius) * start_value,
width - corner_radius, corner_radius + (height - 2 * corner_radius) * start_value,
width - corner_radius, corner_radius + (height - 2 * corner_radius) * end_value,
corner_radius, corner_radius + (height - 2 * corner_radius) * end_value)
elif orientation == "horizontal":
self._canvas.coords("scrollbar_polygon_1",
corner_radius + (width - 2 * corner_radius) * start_value, corner_radius,
corner_radius + (width - 2 * corner_radius) * end_value, corner_radius,
corner_radius + (width - 2 * corner_radius) * end_value, height - corner_radius,
corner_radius + (width - 2 * corner_radius) * start_value, height - corner_radius,)
self._canvas.itemconfig("scrollbar_polygon_1", width=inner_corner_radius * 2)
return requires_recoloring
def __draw_rounded_scrollbar_font_shapes(self, width: int, height: int, corner_radius: int, inner_corner_radius: int,
start_value: float, end_value: float, orientation: str) -> bool:
requires_recoloring = False
if not self._canvas.find_withtag("border_parts"):
self._canvas.create_rectangle(0, 0, 0, 0, tags=("border_rectangle_1", "border_parts"), width=0)
requires_recoloring = True
self._canvas.coords("border_rectangle_1", 0, 0, width, height)
if inner_corner_radius > 0:
if not self._canvas.find_withtag("scrollbar_oval_1_a"):
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_1_a", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_1_b", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
if not self._canvas.find_withtag("scrollbar_oval_2_a") and width > 2 * corner_radius:
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_2_a", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_2_b", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("scrollbar_oval_2_a") and not width > 2 * corner_radius:
self._canvas.delete("scrollbar_oval_2_a", "scrollbar_oval_2_b")
if not self._canvas.find_withtag("scrollbar_oval_3_a") and height > 2 * corner_radius and width > 2 * corner_radius:
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_3_a", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_3_b", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("scrollbar_oval_3_a") and not (height > 2 * corner_radius and width > 2 * corner_radius):
self._canvas.delete("scrollbar_oval_3_a", "scrollbar_oval_3_b")
if not self._canvas.find_withtag("scrollbar_oval_4_a") and height > 2 * corner_radius:
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_4_a", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER)
self._canvas.create_aa_circle(0, 0, 0, tags=("scrollbar_oval_4_b", "scrollbar_corner_part", "scrollbar_parts"), anchor=tkinter.CENTER, angle=180)
requires_recoloring = True
elif self._canvas.find_withtag("scrollbar_oval_4_a") and not height > 2 * corner_radius:
self._canvas.delete("scrollbar_oval_4_a", "scrollbar_oval_4_b")
else:
self._canvas.delete("scrollbar_corner_part")
if not self._canvas.find_withtag("scrollbar_rectangle_1") and height > 2 * corner_radius:
self._canvas.create_rectangle(0, 0, 0, 0, tags=("scrollbar_rectangle_1", "scrollbar_rectangle_part", "scrollbar_parts"), width=0)
requires_recoloring = True
elif self._canvas.find_withtag("scrollbar_rectangle_1") and not height > 2 * corner_radius:
self._canvas.delete("scrollbar_rectangle_1")
if not self._canvas.find_withtag("scrollbar_rectangle_2") and width > 2 * corner_radius:
self._canvas.create_rectangle(0, 0, 0, 0, tags=("scrollbar_rectangle_2", "scrollbar_rectangle_part", "scrollbar_parts"), width=0)
requires_recoloring = True
elif self._canvas.find_withtag("scrollbar_rectangle_2") and not width > 2 * corner_radius:
self._canvas.delete("scrollbar_rectangle_2")
if orientation == "vertical":
self._canvas.coords("scrollbar_rectangle_1",
corner_radius - inner_corner_radius, corner_radius + (height - 2 * corner_radius) * start_value,
width - (corner_radius - inner_corner_radius), corner_radius + (height - 2 * corner_radius) * end_value)
self._canvas.coords("scrollbar_rectangle_2",
corner_radius, corner_radius - inner_corner_radius + (height - 2 * corner_radius) * start_value,
width - (corner_radius), corner_radius + inner_corner_radius + (height - 2 * corner_radius) * end_value)
self._canvas.coords("scrollbar_oval_1_a", corner_radius, corner_radius + (height - 2 * corner_radius) * start_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_1_b", corner_radius, corner_radius + (height - 2 * corner_radius) * start_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_2_a", width - corner_radius, corner_radius + (height - 2 * corner_radius) * start_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_2_b", width - corner_radius, corner_radius + (height - 2 * corner_radius) * start_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_3_a", width - corner_radius, corner_radius + (height - 2 * corner_radius) * end_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_3_b", width - corner_radius, corner_radius + (height - 2 * corner_radius) * end_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_4_a", corner_radius, corner_radius + (height - 2 * corner_radius) * end_value, inner_corner_radius)
self._canvas.coords("scrollbar_oval_4_b", corner_radius, corner_radius + (height - 2 * corner_radius) * end_value, inner_corner_radius)
if orientation == "horizontal":
self._canvas.coords("scrollbar_rectangle_1",
corner_radius - inner_corner_radius + (width - 2 * corner_radius) * start_value, corner_radius,
corner_radius + inner_corner_radius + (width - 2 * corner_radius) * end_value, height - corner_radius)
self._canvas.coords("scrollbar_rectangle_2",
corner_radius + (width - 2 * corner_radius) * start_value, corner_radius - inner_corner_radius,
corner_radius + (width - 2 * corner_radius) * end_value, height - (corner_radius - inner_corner_radius))
self._canvas.coords("scrollbar_oval_1_a", corner_radius + (width - 2 * corner_radius) * start_value, corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_1_b", corner_radius + (width - 2 * corner_radius) * start_value, corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_2_a", corner_radius + (width - 2 * corner_radius) * end_value, corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_2_b", corner_radius + (width - 2 * corner_radius) * end_value, corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_3_a", corner_radius + (width - 2 * corner_radius) * end_value, height - corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_3_b", corner_radius + (width - 2 * corner_radius) * end_value, height - corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_4_a", corner_radius + (width - 2 * corner_radius) * start_value, height - corner_radius, inner_corner_radius)
self._canvas.coords("scrollbar_oval_4_b", corner_radius + (width - 2 * corner_radius) * start_value, height - corner_radius, inner_corner_radius)
return requires_recoloring
def draw_checkmark(self, width: Union[float, int], height: Union[float, 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.

View File

@ -100,6 +100,9 @@ class CTkComboBox(CTkBaseClass):
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
self.entry.delete(0, tkinter.END)
self.entry.insert(0, self.current_value)
self.draw() # initial draw
# event bindings
@ -140,10 +143,6 @@ class CTkComboBox(CTkBaseClass):
self.apply_widget_scaling(self._current_height / 2),
self.apply_widget_scaling(self._current_height / 3))
if self.current_value is not None:
self.entry.delete(0, tkinter.END)
self.entry.insert(0, self.current_value)
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))

View File

@ -1,6 +1,5 @@
import tkinter
import sys
from typing import Union
from .dropdown_menu import DropdownMenu
@ -32,6 +31,7 @@ class CTkOptionMenu(CTkBaseClass):
dropdown_text_font="default_theme",
hover=True,
state=tkinter.NORMAL,
dynamic_resizing=True,
**kwargs):
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
@ -58,6 +58,7 @@ class CTkOptionMenu(CTkBaseClass):
self.variable_callback_name = None
self.state = state
self.hover = hover
self.dynamic_resizing = dynamic_resizing
if values is None:
self.values = ["CTkOptionMenu"]
@ -89,11 +90,14 @@ class CTkOptionMenu(CTkBaseClass):
self.draw_engine = DrawEngine(self.canvas)
left_section_width = self._current_width - self._current_height
self.text_label = tkinter.Label(master=self, font=self.apply_font_scaling(self.text_font))
self.text_label = tkinter.Label(master=self, font=self.apply_font_scaling(self.text_font), anchor="w")
self.text_label.grid(row=0, column=0, sticky="w",
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
if not self.dynamic_resizing:
self.grid_propagate(0)
if Settings.cursor_manipulation_enabled:
if sys.platform == "darwin":
self.configure(cursor="pointinghand")
@ -176,6 +180,8 @@ class CTkOptionMenu(CTkBaseClass):
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
self.canvas.update_idletasks()
def open_dropdown_menu(self):
self.dropdown_menu.open(self.winfo_rootx(),
self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0))
@ -184,47 +190,41 @@ class CTkOptionMenu(CTkBaseClass):
require_redraw = False # some attribute changes require a call of self.draw() at the end
if "state" in kwargs:
self.state = kwargs["state"]
self.state = kwargs.pop("state")
require_redraw = True
del kwargs["state"]
if "fg_color" in kwargs:
self.fg_color = kwargs["fg_color"]
self.fg_color = kwargs.pop("fg_color")
require_redraw = True
del kwargs["fg_color"]
if "bg_color" in kwargs:
if kwargs["bg_color"] is None:
new_bg_color = kwargs.pop("bg_color")
if new_bg_color is None:
self.bg_color = self.detect_color_of_master()
else:
self.bg_color = kwargs["bg_color"]
self.bg_color = new_bg_color
require_redraw = True
del kwargs["bg_color"]
if "button_color" in kwargs:
self.button_color = kwargs["button_color"]
self.button_color = kwargs.pop("button_color")
require_redraw = True
del kwargs["button_color"]
if "button_hover_color" in kwargs:
self.button_hover_color = kwargs["button_hover_color"]
self.button_hover_color = kwargs.pop("button_hover_color")
require_redraw = True
del kwargs["button_hover_color"]
if "text_color" in kwargs:
self.text_color = kwargs["text_color"]
self.text_color = kwargs.pop("text_color")
require_redraw = True
del kwargs["text_color"]
if "command" in kwargs:
self.function = kwargs["command"]
del kwargs["command"]
self.function = kwargs.pop("command")
if "variable" in kwargs:
if self.variable is not None: # remove old callback
self.variable.trace_remove("write", self.variable_callback_name)
self.variable = kwargs["variable"]
self.variable = kwargs.pop("variable")
if self.variable is not None and self.variable != "":
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
@ -232,36 +232,34 @@ class CTkOptionMenu(CTkBaseClass):
else:
self.variable = None
del kwargs["variable"]
if "width" in kwargs:
self.set_dimensions(width=kwargs["width"])
del kwargs["width"]
self.set_dimensions(width=kwargs.pop("width"))
if "height" in kwargs:
self.set_dimensions(height=kwargs["height"])
del kwargs["height"]
self.set_dimensions(height=kwargs.pop("height"))
if "values" in kwargs:
self.values = kwargs["values"]
del kwargs["values"]
self.values = kwargs.pop("values")
self.dropdown_menu.configure(values=self.values)
if "dropdown_color" in kwargs:
self.dropdown_menu.configure(fg_color=kwargs["dropdown_color"])
del kwargs["dropdown_color"]
self.dropdown_menu.configure(fg_color=kwargs.pop("dropdown_color"))
if "dropdown_hover_color" in kwargs:
self.dropdown_menu.configure(hover_color=kwargs["dropdown_hover_color"])
del kwargs["dropdown_hover_color"]
self.dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color"))
if "dropdown_text_color" in kwargs:
self.dropdown_menu.configure(text_color=kwargs["dropdown_text_color"])
del kwargs["dropdown_text_color"]
self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color"))
if "dropdown_text_font" in kwargs:
self.dropdown_menu.configure(text_font=kwargs["dropdown_text_font"])
del kwargs["dropdown_text_font"]
self.dropdown_menu.configure(text_font=kwargs.pop("dropdown_text_font"))
if "dynamic_resizing" in kwargs:
self.dynamic_resizing = kwargs.pop("dynamic_resizing")
if not self.dynamic_resizing:
self.grid_propagate(0)
else:
self.grid_propagate(1)
super().configure(*args, **kwargs)

View File

@ -0,0 +1,238 @@
import sys
from .ctk_canvas import CTkCanvas
from ..theme_manager import ThemeManager
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
class CTkScrollbar(CTkBaseClass):
def __init__(self, *args,
bg_color=None,
fg_color="default_theme",
scrollbar_color="default_theme",
scrollbar_hover_color="default_theme",
border_spacing="default_theme",
corner_radius="default_theme",
width=None,
height=None,
minimum_pixel_length=20,
orientation="vertical",
command=None,
hover=True,
**kwargs):
# set default dimensions according to orientation
if width is None:
if orientation.lower() == "vertical":
width = 16
else:
width = 200
if height is None:
if orientation.lower() == "horizontal":
height = 16
else:
height = 200
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
# color
self.fg_color = ThemeManager.theme["color"]["frame_high"] if fg_color == "default_theme" else fg_color
self.scrollbar_color = ThemeManager.theme["color"]["scrollbar_button"] if scrollbar_color == "default_theme" else scrollbar_color
self.scrollbar_hover_color = ThemeManager.theme["color"]["scrollbar_button_hover"] if scrollbar_hover_color == "default_theme" else scrollbar_hover_color
# shape
self.corner_radius = ThemeManager.theme["shape"]["scrollbar_corner_radius"] if corner_radius == "default_theme" else corner_radius
self.border_spacing = ThemeManager.theme["shape"]["scrollbar_border_spacing"] if border_spacing == "default_theme" else border_spacing
self.hover = hover
self.hover_state = False
self.command = command
self.orientation = orientation
self.start_value: float = 0 # 0 to 1
self.end_value: float = 1 # 0 to 1
self.minimum_pixel_length = minimum_pixel_length
self.canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self.apply_widget_scaling(self._current_width),
height=self.apply_widget_scaling(self._current_height))
self.canvas.place(x=0, y=0, relwidth=1, relheight=1)
self.draw_engine = DrawEngine(self.canvas)
self.canvas.bind("<Enter>", self.on_enter)
self.canvas.bind("<Leave>", self.on_leave)
self.canvas.tag_bind("border_parts", "<Button-1>", self.clicked)
self.canvas.bind("<B1-Motion>", self.clicked)
self.canvas.bind("<MouseWheel>", self.mouse_scroll_event)
self.bind('<Configure>', self.update_dimensions_event)
self.draw()
def set_scaling(self, *args, **kwargs):
super().set_scaling(*args, **kwargs)
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
self.draw(no_color_updates=True)
def set_dimensions(self, width=None, height=None):
super().set_dimensions(width, height)
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
height=self.apply_widget_scaling(self._desired_height))
self.draw(no_color_updates=True)
def get_scrollbar_values_for_minimum_pixel_size(self):
# correct scrollbar float values if scrollbar is too small
if self.orientation == "vertical":
scrollbar_pixel_length = (self.end_value - self.start_value) * self._current_height
if scrollbar_pixel_length < self.minimum_pixel_length and -scrollbar_pixel_length + self._current_height != 0:
# calculate how much to increase the float interval values so that the scrollbar width is self.minimum_pixel_length
interval_extend_factor = (-scrollbar_pixel_length + self.minimum_pixel_length) / (-scrollbar_pixel_length + self._current_height)
corrected_end_value = self.end_value + (1 - self.end_value) * interval_extend_factor
corrected_start_value = self.start_value - self.start_value * interval_extend_factor
return corrected_start_value, corrected_end_value
else:
return self.start_value, self.end_value
else:
scrollbar_pixel_length = (self.end_value - self.start_value) * self._current_width
if scrollbar_pixel_length < self.minimum_pixel_length and -scrollbar_pixel_length + self._current_width != 0:
# calculate how much to increase the float interval values so that the scrollbar width is self.minimum_pixel_length
interval_extend_factor = (-scrollbar_pixel_length + self.minimum_pixel_length) / (-scrollbar_pixel_length + self._current_width)
corrected_end_value = self.end_value + (1 - self.end_value) * interval_extend_factor
corrected_start_value = self.start_value - self.start_value * interval_extend_factor
return corrected_start_value, corrected_end_value
else:
return self.start_value, self.end_value
def draw(self, no_color_updates=False):
corrected_start_value, corrected_end_value = self.get_scrollbar_values_for_minimum_pixel_size()
requires_recoloring = self.draw_engine.draw_rounded_scrollbar(self.apply_widget_scaling(self._current_width),
self.apply_widget_scaling(self._current_height),
self.apply_widget_scaling(self.corner_radius),
self.apply_widget_scaling(self.border_spacing),
corrected_start_value,
corrected_end_value,
self.orientation)
if no_color_updates is False or requires_recoloring:
if self.hover_state is True:
self.canvas.itemconfig("scrollbar_parts",
fill=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode),
outline=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode))
else:
self.canvas.itemconfig("scrollbar_parts",
fill=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode),
outline=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode))
if self.fg_color is None:
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
self.canvas.itemconfig("border_parts",
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
else:
self.canvas.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
self.canvas.itemconfig("border_parts",
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
self.canvas.update_idletasks()
def set(self, start_value: float, end_value: float):
self.start_value = float(start_value)
self.end_value = float(end_value)
self.draw()
def get(self):
return self.start_value, self.end_value
def configure(self, *args, **kwargs):
require_redraw = False # some attribute changes require a call of self.draw() at the end
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 "fg_color" in kwargs:
self.fg_color = kwargs["fg_color"]
require_redraw = True
del kwargs["fg_color"]
if "scrollbar_color" in kwargs:
self.scrollbar_color = kwargs["scrollbar_color"]
require_redraw = True
del kwargs["scrollbar_color"]
if "scrollbar_hover_color" in kwargs:
self.scrollbar_hover_color = kwargs["scrollbar_hover_color"]
require_redraw = True
del kwargs["scrollbar_hover_color"]
if "command" in kwargs:
self.command = kwargs["command"]
del kwargs["command"]
if "corner_radius" in kwargs:
self.corner_radius = kwargs["corner_radius"]
require_redraw = True
del kwargs["corner_radius"]
if "border_spacing" in kwargs:
self.border_spacing = kwargs["border_spacing"]
require_redraw = True
del kwargs["border_spacing"]
if "width" in kwargs:
self.set_dimensions(width=kwargs["width"])
del kwargs["width"]
if "height" in kwargs:
self.set_dimensions(height=kwargs["height"])
del kwargs["height"]
super().configure(*args, **kwargs)
if require_redraw:
self.draw()
def on_enter(self, event=0):
if self.hover is True:
self.hover_state = True
self.canvas.itemconfig("scrollbar_parts",
outline=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode),
fill=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode))
def on_leave(self, event=0):
self.hover_state = False
self.canvas.itemconfig("scrollbar_parts",
outline=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode),
fill=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode))
def clicked(self, event):
if self.orientation == "vertical":
value = ((event.y - self.border_spacing) / (self._current_height - 2 * self.border_spacing)) / self._widget_scaling
else:
value = ((event.x - self.border_spacing) / (self._current_width - 2 * self.border_spacing)) / self._widget_scaling
current_scrollbar_length = self.end_value - self.start_value
value = max(current_scrollbar_length / 2, min(value, 1 - (current_scrollbar_length / 2)))
self.start_value = value - (current_scrollbar_length / 2)
self.end_value = value + (current_scrollbar_length / 2)
self.draw()
if self.command is not None:
self.command('moveto', self.start_value)
def mouse_scroll_event(self, event=None):
if self.command is not None:
if sys.platform.startswith("win"):
self.command('scroll', -int(event.delta/40), 'units')
else:
self.command('scroll', -event.delta, 'units')

View File

@ -61,7 +61,7 @@ class CTkSlider(CTkBaseClass):
self.border_width = ThemeManager.theme["shape"]["slider_border_width"] if border_width == "default_theme" else border_width
self.button_length = ThemeManager.theme["shape"]["slider_button_length"] if button_length == "default_theme" else button_length
self.value = 0.5 # initial value of slider in percent
self.orient = orient
self.orientation = orient
self.hover_state = False
self.from_ = from_
self.to = to
@ -139,9 +139,9 @@ class CTkSlider(CTkBaseClass):
self.configure(cursor="arrow")
def draw(self, no_color_updates=False):
if self.orient.lower() == "horizontal":
if self.orientation.lower() == "horizontal":
orientation = "w"
elif self.orient.lower() == "vertical":
elif self.orientation.lower() == "vertical":
orientation = "s"
else:
orientation = "w"
@ -185,7 +185,7 @@ class CTkSlider(CTkBaseClass):
def clicked(self, event=None):
if self.state == "normal":
if self.orient.lower() == "horizontal":
if self.orientation.lower() == "horizontal":
self.value = (event.x / self._current_width) / self._widget_scaling
else:
self.value = 1 - (event.y / self._current_height) / self._widget_scaling

View File

@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
github_url = "https://github.com/TomSchimansky/CustomTkinter"
[tool.tbump.version]
current = "4.4.1"
current = "4.5.0"
# Example of a semver regexp.
# Make sure this matches current_version before

View File

@ -1,6 +1,6 @@
[metadata]
name = customtkinter
version = 4.4.1
version = 4.5.0
description = Create modern looking GUIs with Python
long_description = CustomTkinter UI-Library\n\n[](https://github.com/TomSchimansky/CustomTkinter/blob/master/documentation_images/Windows_dark.png)\n\nMore Information: https://github.com/TomSchimansky/CustomTkinter
long_description_content_type = text/markdown

View File

@ -12,7 +12,7 @@ def select_callback(choice):
print("display_selected", choice)
countries = ['Bahamas', 'Canada', 'Cuba', 'United States']
countries = ['Bahamas', 'Canada', 'Cuba', 'United States', "long sdhfhjgdshjafghdgshfhjdsfj"]
variable = tkinter.StringVar()
variable.set("test")
@ -23,10 +23,14 @@ optionmenu_tk.pack(pady=10, padx=10)
optionmenu_1 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback)
optionmenu_1.pack(pady=20, padx=10)
optionmenu_2 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback,
dynamic_resizing=False)
optionmenu_2.pack(pady=20, padx=10)
combobox_tk = ttk.Combobox(app, values=countries)
combobox_tk.pack(pady=10, padx=10)
combobox_1 = customtkinter.CTkComboBox(app, variable=variable, values=countries, command=select_callback)
combobox_1 = customtkinter.CTkComboBox(app, variable=None, values=countries, command=select_callback, width=300)
combobox_1.pack(pady=20, padx=10)
app.mainloop()

View File

@ -0,0 +1,54 @@
import tkinter
import customtkinter
# test with scaling
# customtkinter.set_widget_scaling(2)
# customtkinter.set_window_scaling(2)
# customtkinter.set_spacing_scaling(2)
customtkinter.set_appearance_mode("dark")
app = customtkinter.CTk()
app.title("test_scrollbar.py")
app.grid_rowconfigure(0, weight=1)
app.grid_columnconfigure((0, 2), weight=1)
tk_textbox = tkinter.Text(app, highlightthickness=0, padx=5, pady=5)
tk_textbox.grid(row=0, column=0, sticky="nsew")
ctk_textbox_scrollbar = customtkinter.CTkScrollbar(app, command=tk_textbox.yview)
ctk_textbox_scrollbar.grid(row=0, column=1, padx=0, sticky="ns")
tk_textbox.configure(yscrollcommand=ctk_textbox_scrollbar.set)
frame_1 = customtkinter.CTkFrame(app)
frame_1.grid(row=0, column=2, padx=10, pady=10, sticky="nsew")
frame_1.grid_rowconfigure((0, 1), weight=1)
frame_1.grid_columnconfigure((0, ), weight=1)
tk_textbox_1 = tkinter.Text(frame_1, highlightthickness=0, padx=5, pady=5)
tk_textbox_1.grid(row=0, column=0, sticky="nsew", padx=(5, 0), pady=5)
ctk_textbox_scrollbar_1 = customtkinter.CTkScrollbar(frame_1, command=tk_textbox_1.yview)
ctk_textbox_scrollbar_1.grid(row=0, column=1, sticky="ns", padx=(0, 5), pady=5)
tk_textbox_1.configure(yscrollcommand=ctk_textbox_scrollbar_1.set)
ctk_textbox_scrollbar_1.configure(scrollbar_color="red", scrollbar_hover_color="darkred",
border_spacing=0, width=12, fg_color="green", corner_radius=4)
frame_2 = customtkinter.CTkFrame(frame_1)
frame_2.grid(row=1, column=0, columnspan=2, padx=20, pady=20, sticky="nsew")
frame_2.grid_rowconfigure((0, ), weight=1)
frame_2.grid_columnconfigure((0, ), weight=1)
tk_textbox_2 = tkinter.Text(frame_2, highlightthickness=0, padx=5, pady=5, wrap="none")
tk_textbox_2.grid(row=0, column=0, sticky="nsew", padx=(5, 0), pady=5)
ctk_textbox_scrollbar_2 = customtkinter.CTkScrollbar(frame_2, command=tk_textbox_2.yview)
ctk_textbox_scrollbar_2.grid(row=0, column=1, sticky="ns", padx=(0, 5), pady=5)
ctk_textbox_scrollbar_2_horizontal = customtkinter.CTkScrollbar(frame_2, command=tk_textbox_2.xview, orientation="horizontal")
ctk_textbox_scrollbar_2_horizontal.grid(row=1, column=0, sticky="ew", padx=(5, 0), pady=(0, 5))
tk_textbox_2.configure(yscrollcommand=ctk_textbox_scrollbar_2.set, xscrollcommand=ctk_textbox_scrollbar_2_horizontal.set)
tk_textbox.configure(font=(customtkinter.ThemeManager.theme["text"]["font"], customtkinter.ThemeManager.theme["text"]["size"]))
tk_textbox_1.configure(font=(customtkinter.ThemeManager.theme["text"]["font"], customtkinter.ThemeManager.theme["text"]["size"]))
tk_textbox_2.configure(font=(customtkinter.ThemeManager.theme["text"]["font"], customtkinter.ThemeManager.theme["text"]["size"]))
tk_textbox.insert("insert", "\n".join([str(i) for i in range(100)]))
tk_textbox_1.insert("insert", "\n".join([str(i) for i in range(1000)]))
tk_textbox_2.insert("insert", "\n".join([str(i) + " - "*30 for i in range(10000)]))
app.mainloop()