Search code examples
pythontkintercustomtkintertkinter-text

Customtkinter: Change text color of switch


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()

Solution

  • 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()