Search code examples
colorskivykivymdtextinput

KivyMD setting colors for hint_text, text, error_text do not seem to work


I have working code (using python 3.9, kivy 2.1.0, kivymd 1.1.1) but cannot get it to set any of the colors that are supposed to be settable via KV keywords from the KivyMD Documentation.

I have tried: line_color_focus, background_color, disabled_foreground_color, foreground_color, hint_text_color, and selection_color. Are there any others to try? I am really trying to get the hint text and input text to be black or white depending on background color.

from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = '''
<IconListItem>
    IconLeftWidget:
        icon: root.icon

MDScreen:
    MDTextField:
        id: waveform
        pos_hint: {'center_x': .3, 'center_y': .6}
        size_hint_x: None
        width: "250dp"
        font_size: '25sp'
        line_color_focus: 0, 0, 0, 1
        required: True
        hint_text: "Wave Form"
        keyboard_mode: "managed"       
        on_focus: if self.focus: {app.open_menu(self, app.menu1), app.set_cust_labels()}

    MDTextField:
        id: freq1
        input_filter: "float"
        input_type: "number"
        pos_hint: {'center_x': .3, 'center_y': .5}
        size_hint_x: None
        width: "250dp"
        font_size: '25sp'
        multiline: False
        line_color_focus: 0, 0, 0, 1
        required: True
        hint_text: "Start Frequency in Hz"
        helper_text: "Enter a number between .01 and 20000"
        helper_text_mode: "persistent"
        on_focus: if self.focus: app.set_cust_labels()


    MDBoxLayout:
        orientation: "horizontal"
        pos_hint: {'center_x': .55, 'center_y': .2}
        MDLabel:
            id: waveform_text
            valign: "center"
            font_size: '25sp'
            theme_text_color: "Primary"

        MDLabel:
            id: freq1_text
            valign: "center"
            font_size: '25sp'
            theme_text_color: "Primary"
'''

class IconListItem(OneLineIconListItem):
    icon = StringProperty()

class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)

        self.wf_list_items = ["Sine", "Rectangle", "Sawtooth", "Impulse", "Bemer"]

        menu_items_wf = [
            {
                "viewclass": "IconListItem",
                "icon": "account",
                "height": dp(56),
                "text": f"{self.wf_list_items[i]}",
                "on_release": lambda x=i: self.set_item_waveform(f"{self.wf_list_items[x]}"),
            } for i in range(5)]

        self.menu1 = MDDropdownMenu(
            items=menu_items_wf,
            position="auto",
            width_mult=3,
        )

    def set_item_waveform(self, text__item):
        self.screen.ids.waveform.text = text__item
        self.screen.ids.waveform.focus = False
        self.menu1.dismiss()

    def open_menu(self, textfield, menu):
        textfield.focus = True
        menu.width_mult = textfield.width / dp(55)
        menu.max_height = dp(300)
        menu.caller = textfield
        menu.open()
        textfield.focus = False

    def set_cust_labels(self):
        self.screen.ids.waveform_text.text = self.screen.ids.waveform.text
        self.screen.ids.freq1_text.text = self.screen.ids.freq1.text

    def build(self):
        return self.screen

Test().run()

Does anyone have some suggestions on how to set text color to black? I have had no luck using any of the color settings in MDTextField.

line_color_focus: 0, 0, 0, 1 <- in code doesn't work
background_color: 0, 0, 0, 1
disabled_foreground_color: 0,0,0 1
foreground_color: 0, 0, 0, 1
hint_text_color: 0, 0, 0, 1
selection_color: 0, 0, 0, 1

Each of those do not seem to set any of the text items to black. Any reason why?


Solution

  • Have a look at the documentation. The attributes for text color are text_color_normal and text_color_focus. Similarly, for hint text: hint_text_color_normal and hint_text_color_focus. The kivymd docs are a good source of that sort of information.