fixed some configure bugs #585 #584 #583 #580 #579 #578, added check_image method

This commit is contained in:
Tom Schimansky
2022-11-06 14:40:15 +01:00
parent 62b330ddba
commit cea48c3501
16 changed files with 144 additions and 102 deletions

View File

@ -12,6 +12,7 @@ from ...ctk_tk import CTk
from ...ctk_toplevel import CTkToplevel
from ..theme.theme_manager import ThemeManager
from ..font.ctk_font import CTkFont
from ..image.ctk_image import CTkImage
from ..appearance_mode.appearance_mode_base_class import CTkAppearanceModeBaseClass
from ..scaling.scaling_base_class import CTkScalingBaseClass
@ -145,13 +146,13 @@ class CTkBaseClass(tkinter.Frame, CTkAppearanceModeBaseClass, CTkScalingBaseClas
else:
raise ValueError(f"'{attribute_name}' is not a supported argument. Look at the documentation for supported arguments.")
@staticmethod
def _check_font_type(font: any):
def _check_font_type(self, font: any):
""" check font type when passed to widget """
if isinstance(font, CTkFont):
return font
elif type(font) == tuple and len(font) == 1:
sys.stderr.write(f"Warning: font {font} given without size, will be extended with default text size of current theme\n")
sys.stderr.write(f"{type(self).__name__} Warning: font {font} given without size, will be extended with default text size of current theme\n")
return font[0], ThemeManager.theme["text"]["size"]
elif type(font) == tuple and 2 <= len(font) <= 3:
@ -164,6 +165,17 @@ class CTkBaseClass(tkinter.Frame, CTkAppearanceModeBaseClass, CTkScalingBaseClas
f"font=customtkinter.CTkFont(family='<name>', size=<size in px>)\n" +
f"font=('<name>', <size in px>)\n")
def _check_image_type(self, image: any):
""" check image type when passed to widget """
if image is None:
return image
elif isinstance(image, CTkImage):
return image
else:
sys.stderr.write(f"{type(self).__name__} Warning: Given image is not CTkImage but {type(image)}. " +
f"Image can not be scaled on HighDPI displays, use CTkImage instead.\n")
return image
def _update_dimensions_event(self, event):
# only redraw if dimensions changed (for performance), independent of scaling
if round(self._current_width) != round(self._reverse_widget_scaling(event.width)) or round(self._current_height) != round(self._reverse_widget_scaling(event.height)):

View File

@ -70,7 +70,7 @@ class CTkButton(CTkBaseClass):
self._border_spacing = border_spacing
# text, image
self._image = image
self._image = self._check_image_type(image)
self._image_label: Union[tkinter.Label, None] = None
self._text = text
self._text_label: Union[tkinter.Label, None] = None
@ -381,7 +381,7 @@ class CTkButton(CTkBaseClass):
if "image" in kwargs:
if isinstance(self._image, CTkImage):
self._image.remove_configure_callback(self._update_image)
self._image = kwargs.pop("image")
self._image = self._check_image_type(kwargs.pop("image"))
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
require_redraw = True

View File

@ -216,6 +216,14 @@ class CTkCheckBox(CTkBaseClass):
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
def configure(self, require_redraw=False, **kwargs):
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
require_redraw = True
if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True
if "checkbox_width" in kwargs:
self._checkbox_width = kwargs.pop("checkbox_width")
self._canvas.configure(width=self._apply_widget_scaling(self._checkbox_width))

View File

@ -23,7 +23,7 @@ class CTkLabel(CTkBaseClass):
def __init__(self,
master: any = None,
width: int = 140,
width: int = 0,
height: int = 28,
corner_radius: Union[int, str] = "default_theme",
@ -53,7 +53,7 @@ class CTkLabel(CTkBaseClass):
self._text = text
# image
self._image = image
self._image = self._check_image_type(image)
self._compound = compound
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
@ -85,12 +85,9 @@ class CTkLabel(CTkBaseClass):
font=self._apply_font_scaling(self._font))
self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes))
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
check_kwargs_empty(kwargs, raise_error=True)
self._create_grid()
self._update_image()
self._draw()
@ -100,10 +97,7 @@ class CTkLabel(CTkBaseClass):
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width), height=self._apply_widget_scaling(self._desired_height))
self._label.configure(font=self._apply_font_scaling(self._font))
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
self._create_grid()
self._draw(no_color_updates=True)
def _set_dimensions(self, width=None, height=None):
@ -111,6 +105,7 @@ class CTkLabel(CTkBaseClass):
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._create_grid()
self._draw()
def _update_font(self):
@ -134,6 +129,13 @@ class CTkLabel(CTkBaseClass):
self._font.remove_size_configure_callback(self._update_font)
super().destroy()
def _create_grid(self):
""" configure grid system (1x1) """
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height / 2))))
def _draw(self, no_color_updates=False):
super()._draw(no_color_updates)
@ -164,15 +166,18 @@ class CTkLabel(CTkBaseClass):
self._update_image()
def configure(self, require_redraw=False, **kwargs):
if "anchor" in kwargs:
self._anchor = kwargs.pop("anchor")
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
self._create_grid()
require_redraw = True
if "compound" in kwargs:
self._compound = kwargs.pop("compound")
self._label.configure(compound=self._compound)
if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True
if "text_color" in kwargs:
self._text_color = kwargs.pop("text_color")
require_redraw = True
if "text" in kwargs:
self._text = kwargs.pop("text")
@ -189,25 +194,19 @@ class CTkLabel(CTkBaseClass):
if "image" in kwargs:
if isinstance(self._image, CTkImage):
self._image.remove_configure_callback(self._update_image)
self._image = kwargs.pop("image")
self._image = self._check_image_type(kwargs.pop("image"))
if isinstance(self._image, CTkImage):
self._image.add_configure_callback(self._update_image)
self._update_image()
if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True
if "compound" in kwargs:
self._compound = kwargs.pop("compound")
self._label.configure(compound=self._compound)
if "text_color" in kwargs:
self._text_color = kwargs.pop("text_color")
require_redraw = True
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
require_redraw = True
if "anchor" in kwargs:
self._anchor = kwargs.pop("anchor")
self._label.configure(anchor=self._anchor)
self._create_grid()
self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes)) # configure tkinter.Label
super().configure(require_redraw=require_redraw, **kwargs) # configure CTkBaseClass

View File

@ -153,6 +153,14 @@ class CTkProgressBar(CTkBaseClass):
outline=self._apply_appearance_mode(self._progress_color))
def configure(self, require_redraw=False, **kwargs):
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
require_redraw = True
if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True
if "fg_color" in kwargs:
self._fg_color = kwargs.pop("fg_color")
require_redraw = True
@ -165,10 +173,6 @@ class CTkProgressBar(CTkBaseClass):
self._progress_color = kwargs.pop("progress_color")
require_redraw = True
if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True
if "variable" in kwargs:
if self._variable is not None:
self._variable.trace_remove("write", self._variable_callback_name)

View File

@ -58,7 +58,6 @@ class CTkRadioButton(CTkBaseClass):
self._corner_radius = ThemeManager.theme["shape"]["radiobutton_corner_radius"] if corner_radius == "default_theme" else corner_radius
self._border_width_unchecked = ThemeManager.theme["shape"]["radiobutton_border_width_unchecked"] if border_width_unchecked == "default_theme" else border_width_unchecked
self._border_width_checked = ThemeManager.theme["shape"]["radiobutton_border_width_checked"] if border_width_checked == "default_theme" else border_width_checked
self._border_width = self._border_width_unchecked
# text
self._text = text
@ -165,10 +164,16 @@ class CTkRadioButton(CTkBaseClass):
def _draw(self, no_color_updates=False):
super()._draw(no_color_updates)
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._radiobutton_width),
self._apply_widget_scaling(self._radiobutton_height),
self._apply_widget_scaling(self._corner_radius),
self._apply_widget_scaling(self._border_width))
if self._check_state is True:
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._radiobutton_width),
self._apply_widget_scaling(self._radiobutton_height),
self._apply_widget_scaling(self._corner_radius),
self._apply_widget_scaling(self._border_width_checked))
else:
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._radiobutton_width),
self._apply_widget_scaling(self._radiobutton_height),
self._apply_widget_scaling(self._corner_radius),
self._apply_widget_scaling(self._border_width_unchecked))
if no_color_updates is False or requires_recoloring:
self._bg_canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
@ -195,6 +200,18 @@ class CTkRadioButton(CTkBaseClass):
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
def configure(self, require_redraw=False, **kwargs):
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")
require_redraw = True
if "border_width_unchecked" in kwargs:
self._border_width_unchecked = kwargs.pop("border_width_unchecked")
require_redraw = True
if "border_width_checked" in kwargs:
self._border_width_checked = kwargs.pop("border_width_checked")
require_redraw = True
if "radiobutton_width" in kwargs:
self._radiobutton_width = kwargs.pop("radiobutton_width")
self._canvas.configure(width=self._apply_widget_scaling(self._radiobutton_width))
@ -239,10 +256,6 @@ class CTkRadioButton(CTkBaseClass):
self._border_color = kwargs.pop("border_color")
require_redraw = True
if "border_width" in kwargs:
self._border_width = kwargs.pop("border_width")
require_redraw = True
if "hover" in kwargs:
self._hover = kwargs.pop("hover")
@ -365,7 +378,6 @@ class CTkRadioButton(CTkBaseClass):
def select(self, from_variable_callback=False):
self._check_state = True
self._border_width = self._border_width_checked
self._draw()
if self._variable is not None and not from_variable_callback:
@ -375,7 +387,6 @@ class CTkRadioButton(CTkBaseClass):
def deselect(self, from_variable_callback=False):
self._check_state = False
self._border_width = self._border_width_unchecked
self._draw()
if self._variable is not None and not from_variable_callback:

View File

@ -17,6 +17,7 @@ class CTkTabview(CTkBaseClass):
_top_spacing = 10 # px on top of the buttons
_top_button_overhang = 8 # px
_button_height = 26
_segmented_button_border_width = 3
def __init__(self,
master: any = None,
@ -67,8 +68,8 @@ class CTkTabview(CTkBaseClass):
self._canvas = CTkCanvas(master=self,
bg=self._apply_appearance_mode(self._bg_color),
highlightthickness=0,
width=self._apply_widget_scaling(self._current_width - self._top_spacing - self._top_button_overhang),
height=self._apply_widget_scaling(self._current_height))
width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height - self._top_spacing - self._top_button_overhang))
self._draw_engine = DrawEngine(self._canvas)
self._segmented_button = CTkSegmentedButton(self,
@ -82,7 +83,7 @@ class CTkTabview(CTkBaseClass):
text_color=text_color,
text_color_disabled=text_color_disabled,
corner_radius=corner_radius,
border_width=self._apply_widget_scaling(3),
border_width=self._apply_widget_scaling(self._segmented_button_border_width),
command=self._segmented_button_callback,
state=state)
self._configure_segmented_button_background_corners()
@ -143,14 +144,6 @@ class CTkTabview(CTkBaseClass):
def _configure_tab_background_corners_by_name(self, name: str):
""" needs to be called for changes in fg_color, bg_color, border_width """
# if self._border_width == 0:
# if self._fg_color is not None:
# self._tab_dict[name].configure(background_corner_colors=(self._fg_color, self._fg_color, self._bg_color, self._bg_color))
# else:
# self._tab_dict[name].configure(background_corner_colors=(self._bg_color, self._bg_color, self._bg_color, self._bg_color))
# else:
# self._tab_dict[name].configure(background_corner_colors=None)
self._tab_dict[name].configure(background_corner_colors=None)
def _configure_grid(self):
@ -182,6 +175,8 @@ class CTkTabview(CTkBaseClass):
def _create_tab(self) -> CTkFrame:
new_tab = CTkFrame(self,
height=0,
width=0,
fg_color=self._fg_color,
border_width=0,
corner_radius=self._corner_radius)

View File

@ -73,8 +73,8 @@ class CTkTextbox(CTkBaseClass):
self._canvas = CTkCanvas(master=self,
highlightthickness=0,
width=self._apply_widget_scaling(self._current_width),
height=self._apply_widget_scaling(self._current_height))
width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew")
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
self._draw_engine = DrawEngine(self._canvas)