Skip to content

Commit db08925

Browse files
FedericoSpadaShubham25decfredmain
committed
Improved/fixed configure() method for all widgets
- Added missing attributes to the configure() method for all widgets. - Allowed changing a CTkButton image using configure even if the widget was created without any image. - Prevented warning when an image is set to "" with configure() to delete it. - Fixed CTkCheckbox hover_color for the blue theme, which was hiding the hover effect when checked. Fixes #1215, #1750, #2494 and replaces #2412, #2719 Co-Authored-By: Shubham25dec <107737999+shubham25dec@users.noreply.github.com> Co-Authored-By: fred Jose Diaz <33442727+fredmain@users.noreply.github.com>
1 parent 6a5460b commit db08925

20 files changed

+216
-190
lines changed

customtkinter/assets/themes/blue.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"border_width": 3,
4242
"fg_color": ["#3B8ED0", "#1F6AA5"],
4343
"border_color": ["#3E454A", "#949A9F"],
44-
"hover_color": ["#3B8ED0", "#1F6AA5"],
44+
"hover_color": ["#36719F", "#144870"],
4545
"checkmark_color": ["#DCE4EE", "gray90"],
4646
"text_color": ["gray10", "#DCE4EE"],
4747
"text_color_disabled": ["gray60", "gray45"]

customtkinter/windows/widgets/core_widget_classes/ctk_base_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _check_font_type(self, font: any):
171171

172172
def _check_image_type(self, image: any):
173173
""" check image type when passed to widget """
174-
if image is None:
174+
if image is None or image == "":
175175
return image
176176
elif isinstance(image, CTkImage):
177177
return image

customtkinter/windows/widgets/core_widget_classes/dropdown_menu.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def open(self, x: Union[int, float], y: Union[int, float]):
120120
self.tk_popup(int(x), int(y))
121121

122122
def configure(self, **kwargs):
123+
if "min_character_width" in kwargs:
124+
self._min_character_width = kwargs.pop("min_character_width")
125+
self._add_menu_commands()
126+
123127
if "fg_color" in kwargs:
124128
self._fg_color = self._check_color_type(kwargs.pop("fg_color"))
125129
super().configure(bg=self._apply_appearance_mode(self._fg_color))
@@ -138,7 +142,6 @@ def configure(self, **kwargs):
138142
self._font = self._check_font_type(kwargs.pop("font"))
139143
if isinstance(self._font, CTkFont):
140144
self._font.add_size_configure_callback(self._update_font)
141-
142145
self._update_font()
143146

144147
if "command" in kwargs:

customtkinter/windows/widgets/ctk_button.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ def configure(self, require_redraw=False, **kwargs):
402402
self._font = self._check_font_type(kwargs.pop("font"))
403403
if isinstance(self._font, CTkFont):
404404
self._font.add_size_configure_callback(self._update_font)
405-
406405
self._update_font()
407406

408407
if "textvariable" in kwargs:
@@ -416,7 +415,10 @@ def configure(self, require_redraw=False, **kwargs):
416415
self._image = self._check_image_type(kwargs.pop("image"))
417416
if isinstance(self._image, CTkImage):
418417
self._image.add_configure_callback(self._update_image)
419-
self._update_image()
418+
if self._image_label is not None:
419+
self._update_image()
420+
else:
421+
require_redraw = True
420422

421423
if "state" in kwargs:
422424
self._state = kwargs.pop("state")

customtkinter/windows/widgets/ctk_checkbox.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,7 @@ def _draw(self, no_color_updates=False):
221221
self._text_label.configure(bg=self._apply_appearance_mode(self._bg_color))
222222

223223
def configure(self, require_redraw=False, **kwargs):
224-
if "corner_radius" in kwargs:
225-
self._corner_radius = kwargs.pop("corner_radius")
226-
require_redraw = True
227-
228-
if "border_width" in kwargs:
229-
self._border_width = kwargs.pop("border_width")
230-
require_redraw = True
224+
require_new_state = False
231225

232226
if "checkbox_width" in kwargs:
233227
self._checkbox_width = kwargs.pop("checkbox_width")
@@ -239,22 +233,12 @@ def configure(self, require_redraw=False, **kwargs):
239233
self._canvas.configure(height=self._apply_widget_scaling(self._checkbox_height))
240234
require_redraw = True
241235

242-
if "text" in kwargs:
243-
self._text = kwargs.pop("text")
244-
self._text_label.configure(text=self._text)
245-
246-
if "font" in kwargs:
247-
if isinstance(self._font, CTkFont):
248-
self._font.remove_size_configure_callback(self._update_font)
249-
self._font = self._check_font_type(kwargs.pop("font"))
250-
if isinstance(self._font, CTkFont):
251-
self._font.add_size_configure_callback(self._update_font)
252-
253-
self._update_font()
236+
if "corner_radius" in kwargs:
237+
self._corner_radius = kwargs.pop("corner_radius")
238+
require_redraw = True
254239

255-
if "state" in kwargs:
256-
self._state = kwargs.pop("state")
257-
self._set_cursor()
240+
if "border_width" in kwargs:
241+
self._border_width = kwargs.pop("border_width")
258242
require_redraw = True
259243

260244
if "fg_color" in kwargs:
@@ -281,27 +265,52 @@ def configure(self, require_redraw=False, **kwargs):
281265
self._text_color_disabled = self._check_color_type(kwargs.pop("text_color_disabled"))
282266
require_redraw = True
283267

268+
if "text" in kwargs:
269+
self._text = kwargs.pop("text")
270+
self._text_label.configure(text=self._text)
271+
272+
if "font" in kwargs:
273+
if isinstance(self._font, CTkFont):
274+
self._font.remove_size_configure_callback(self._update_font)
275+
self._font = self._check_font_type(kwargs.pop("font"))
276+
if isinstance(self._font, CTkFont):
277+
self._font.add_size_configure_callback(self._update_font)
278+
self._update_font()
279+
280+
if "textvariable" in kwargs:
281+
self._textvariable = kwargs.pop("textvariable")
282+
self._text_label.configure(textvariable=self._textvariable)
283+
284+
if "state" in kwargs:
285+
self._state = kwargs.pop("state")
286+
self._set_cursor()
287+
require_redraw = True
288+
284289
if "hover" in kwargs:
285290
self._hover = kwargs.pop("hover")
286291

287292
if "command" in kwargs:
288293
self._command = kwargs.pop("command")
289294

290-
if "textvariable" in kwargs:
291-
self._textvariable = kwargs.pop("textvariable")
292-
self._text_label.configure(textvariable=self._textvariable)
295+
if "onvalue" in kwargs:
296+
self._onvalue = kwargs.pop("onvalue")
297+
require_new_state = True
298+
299+
if "offvalue" in kwargs:
300+
self._offvalue = kwargs.pop("offvalue")
301+
require_new_state = True
293302

294303
if "variable" in kwargs:
295304
if self._variable is not None and self._variable != "":
296305
self._variable.trace_remove("write", self._variable_callback_name) # remove old variable callback
297-
298306
self._variable = kwargs.pop("variable")
299-
300307
if self._variable is not None and self._variable != "":
301308
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
302-
self._check_state = True if self._variable.get() == self._onvalue else False
303-
require_redraw = True
309+
require_new_state = True
304310

311+
if require_new_state and self._variable is not None and self._variable != "":
312+
self._check_state = True if self._variable.get() == self._onvalue else False
313+
require_redraw = True
305314
super().configure(require_redraw=require_redraw, **kwargs)
306315

307316
def cget(self, attribute_name: str) -> any:

customtkinter/windows/widgets/ctk_combobox.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ def configure(self, require_redraw=False, **kwargs):
267267
self._font = self._check_font_type(kwargs.pop("font"))
268268
if isinstance(self._font, CTkFont):
269269
self._font.add_size_configure_callback(self._update_font)
270-
271270
self._update_font()
272271

273272
if "dropdown_font" in kwargs:

customtkinter/windows/widgets/ctk_entry.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,36 +192,36 @@ def _draw(self, no_color_updates=False):
192192
insertbackground=self._apply_appearance_mode(self._text_color))
193193

194194
def configure(self, require_redraw=False, **kwargs):
195-
if "state" in kwargs:
196-
self._state = kwargs.pop("state")
197-
self._entry.configure(state=self._state)
198-
199-
if "fg_color" in kwargs:
200-
self._fg_color = self._check_color_type(kwargs.pop("fg_color"))
195+
if "corner_radius" in kwargs:
196+
self._corner_radius = kwargs.pop("corner_radius")
197+
self._create_grid()
201198
require_redraw = True
202199

203-
if "text_color" in kwargs:
204-
self._text_color = self._check_color_type(kwargs.pop("text_color"))
200+
if "border_width" in kwargs:
201+
self._border_width = kwargs.pop("border_width")
202+
self._create_grid()
205203
require_redraw = True
206204

207-
if "placeholder_text_color" in kwargs:
208-
self._placeholder_text_color = self._check_color_type(kwargs.pop("placeholder_text_color"))
205+
if "fg_color" in kwargs:
206+
self._fg_color = self._check_color_type(kwargs.pop("fg_color"))
209207
require_redraw = True
210208

211209
if "border_color" in kwargs:
212210
self._border_color = self._check_color_type(kwargs.pop("border_color"))
213211
require_redraw = True
214212

215-
if "border_width" in kwargs:
216-
self._border_width = kwargs.pop("border_width")
217-
self._create_grid()
213+
if "text_color" in kwargs:
214+
self._text_color = self._check_color_type(kwargs.pop("text_color"))
218215
require_redraw = True
219216

220-
if "corner_radius" in kwargs:
221-
self._corner_radius = kwargs.pop("corner_radius")
222-
self._create_grid()
217+
if "placeholder_text_color" in kwargs:
218+
self._placeholder_text_color = self._check_color_type(kwargs.pop("placeholder_text_color"))
223219
require_redraw = True
224220

221+
if "textvariable" in kwargs:
222+
self._textvariable = kwargs.pop("textvariable")
223+
self._entry.configure(textvariable=self._textvariable)
224+
225225
if "placeholder_text" in kwargs:
226226
self._placeholder_text = kwargs.pop("placeholder_text")
227227
if self._placeholder_text_active:
@@ -230,19 +230,18 @@ def configure(self, require_redraw=False, **kwargs):
230230
else:
231231
self._activate_placeholder()
232232

233-
if "textvariable" in kwargs:
234-
self._textvariable = kwargs.pop("textvariable")
235-
self._entry.configure(textvariable=self._textvariable)
236-
237233
if "font" in kwargs:
238234
if isinstance(self._font, CTkFont):
239235
self._font.remove_size_configure_callback(self._update_font)
240236
self._font = self._check_font_type(kwargs.pop("font"))
241237
if isinstance(self._font, CTkFont):
242238
self._font.add_size_configure_callback(self._update_font)
243-
244239
self._update_font()
245240

241+
if "state" in kwargs:
242+
self._state = kwargs.pop("state")
243+
self._entry.configure(state=self._state)
244+
246245
if "show" in kwargs:
247246
if self._placeholder_text_active:
248247
self._pre_placeholder_arguments["show"] = kwargs.pop("show") # remember show argument for when placeholder gets deactivated

customtkinter/windows/widgets/ctk_frame.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def _draw(self, no_color_updates=False):
132132
# self._canvas.tag_lower("border_parts")
133133

134134
def configure(self, require_redraw=False, **kwargs):
135+
if "corner_radius" in kwargs:
136+
self._corner_radius = kwargs.pop("corner_radius")
137+
require_redraw = True
138+
139+
if "border_width" in kwargs:
140+
self._border_width = kwargs.pop("border_width")
141+
require_redraw = True
142+
135143
if "fg_color" in kwargs:
136144
self._fg_color = self._check_color_type(kwargs.pop("fg_color"), transparency=True)
137145
require_redraw = True
@@ -156,14 +164,6 @@ def configure(self, require_redraw=False, **kwargs):
156164
self._background_corner_colors = kwargs.pop("background_corner_colors")
157165
require_redraw = True
158166

159-
if "corner_radius" in kwargs:
160-
self._corner_radius = kwargs.pop("corner_radius")
161-
require_redraw = True
162-
163-
if "border_width" in kwargs:
164-
self._border_width = kwargs.pop("border_width")
165-
require_redraw = True
166-
167167
super().configure(require_redraw=require_redraw, **kwargs)
168168

169169
def cget(self, attribute_name: str) -> any:

customtkinter/windows/widgets/ctk_optionmenu.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ def configure(self, require_redraw=False, **kwargs):
262262
self._font = self._check_font_type(kwargs.pop("font"))
263263
if isinstance(self._font, CTkFont):
264264
self._font.add_size_configure_callback(self._update_font)
265-
266265
self._update_font()
267266

268267
if "dropdown_font" in kwargs:
@@ -275,15 +274,11 @@ def configure(self, require_redraw=False, **kwargs):
275274
if "variable" in kwargs:
276275
if self._variable is not None: # remove old callback
277276
self._variable.trace_remove("write", self._variable_callback_name)
278-
279277
self._variable = kwargs.pop("variable")
280-
281278
if self._variable is not None and self._variable != "":
282279
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
283280
self._current_value = self._variable.get()
284281
self._text_label.configure(text=self._current_value)
285-
else:
286-
self._variable = None
287282

288283
if "state" in kwargs:
289284
self._state = kwargs.pop("state")

customtkinter/windows/widgets/ctk_progressbar.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,10 @@ def configure(self, require_redraw=False, **kwargs):
181181
if "variable" in kwargs:
182182
if self._variable is not None:
183183
self._variable.trace_remove("write", self._variable_callback_name)
184-
185184
self._variable = kwargs.pop("variable")
186-
187185
if self._variable is not None and self._variable != "":
188186
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
189187
self.set(self._variable.get(), from_variable_callback=True)
190-
else:
191-
self._variable = None
192188

193189
if "mode" in kwargs:
194190
self._mode = kwargs.pop("mode")

0 commit comments

Comments
 (0)