1

I want to change the text color of the switch widget text in customtkinter

I tried to use configure in with text_color but it showed me that there's no attribute of a switch called text_color...

Btw.. when creating the switch text_color works

minimal reproducible example:

import customtkinter as ctk  
root = ctk.CTk()
switch = ctk.CTkSwitch(master=root, text='This is a switch', text_color='yellow')
switch.pack()
switch.configure(text_color='red')
root.mainloop()
figbeam
  • 7,001
  • 2
  • 12
  • 18
Adrian8115
  • 17
  • 4

3 Answers3

0

Customtkinter is installed out of the box only with the .configure settings:

def configure(self, require_redraw=False, **kwargs):
        """ basic configure with bg_color, width, height support, calls configure of tkinter.Frame, updates in the end """ 

that you can see in the ctk_base_class.py file which crashes during an error.

Traceback (most recent call last):
  File "C:\Users\ф\PycharmProjects\tkinter\rrr.py", line 5, in <module>
    switch.configure(text_color='red')
  File "C:\Users\ф\PycharmProjects\tkinter\venv\Lib\site-packages\customtkinter\windows\widgets\ctk_switch.py", line 339, in configure
    super().configure(require_redraw=require_redraw, **kwargs)
  File "C:\Users\ф\PycharmProjects\tkinter\venv\Lib\site-packages\customtkinter\windows\widgets\core_widget_classes\ctk_base_class.py", line 137, in configure
    check_kwargs_empty(kwargs, raise_error=True)
  File "C:\Users\ф\PycharmProjects\tkinter\venv\Lib\site-packages\customtkinter\windows\widgets\utility\utility_functions.py", line 18, in check_kwargs_empty
    raise ValueError(f"{list(kwargs_dict.keys())} are not supported arguments. Look at the documentation for supported arguments.")
ValueError: ['text_color'] are not supported arguments. Look at the documentation for supported arguments.

This is probably due to the constant development of the library at the present time. To add the functionality you need, you can copy the lines you need to github and add them to a file on your computer.

if "text_color" in kwargs:
            self._text_color = self._check_color_type(kwargs.pop("text_color"))
            require_redraw = True

Then the method in the ctk_base_class.py file will look like this:

    def configure(self, require_redraw=False, **kwargs):
        """ basic configure with bg_color, width, height support, calls configure of tkinter.Frame, updates in the end """

        if "width" in kwargs:
            self._set_dimensions(width=kwargs.pop("width"))

        if "height" in kwargs:
            self._set_dimensions(height=kwargs.pop("height"))

        if "bg_color" in kwargs:
            new_bg_color = self._check_color_type(kwargs.pop("bg_color"), transparency=True)
            if new_bg_color == "transparent":
                self._bg_color = self._detect_color_of_master()
            else:
                self._bg_color = self._check_color_type(new_bg_color)
            require_redraw = True

        if "text_color" in kwargs:
            self._text_color = self._check_color_type(kwargs.pop("text_color"))
            require_redraw = True

        super().configure(**pop_from_dict_by_set(kwargs, self._valid_tk_frame_attributes))  # configure tkinter.Frame

        # if there are still items in the kwargs dict, raise ValueError
        check_kwargs_empty(kwargs, raise_error=True)

        if require_redraw:
            self._draw()
Сергей Кох
  • 1,417
  • 12
  • 6
  • 13
  • 1
    From design point of view, it is not a good design to configure *"text_color"* inside `ctk_base_class.py` because `CTkBaseClass` inherits from `tkinter.Frame` and there is no text color for a frame. Actually `CTkSwitch` class (in github) has been updated to address this issue. – acw1668 May 02 '23 at 15:32
  • @acw1668 I agree with you. I leave the answer as an option - "look inside" when something does not work. – Сергей Кох May 02 '23 at 15:37
  • Its fixed now with version 5.1.3. – Tom May 05 '23 at 18:21
0

Configuring CTkSwitch.configure(text_color=...) works in CustomTkinter 5.1.3.

Clive Bostock
  • 150
  • 1
  • 11
-1

You can access the label attribute of ctkswitch object and call the 'config' method which has a 'fg' parameter. This changes the foreground text colour of the label which should work.

Replace:

switch.configure(text_color='red')

With:

switch.label.config(fg='red')
FreddyC08
  • 51
  • 2