KivyMD中MDTextField提示文本、输入文本颜色设置失效求助
问题
使用Python 3.9、Kivy 2.1.0、KivyMD 1.1.1编写的代码可正常运行,但无法通过KivyMD文档中的KV关键字设置MDTextField的提示文本、输入文本等颜色。尝试line_color_focus、background_color、disabled_foreground_color、foreground_color、hint_text_color、selection_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()
尝试过以下属性均无法将文本设为黑色:
line_color_focus: 0, 0, 0, 1 <- 代码中无效 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
请问这些颜色设置属性为何不生效?有什么办法可以将文本颜色设为黑色?
解决方案
属性不生效原因
KivyMD的MDTextField默认继承主题样式,直接设置的颜色属性会被主题的palette和内置文本颜色规则覆盖;另外部分属性(如foreground_color)在对应版本中已被废弃,不再生效。
自定义颜色的有效方法
要修改输入文本、提示文本颜色,需先将theme_text_color设为"Custom",再指定对应的颜色属性,这样可以绕过主题样式的优先级限制。
修改后的完整代码如下:
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" # 开启自定义文本颜色模式 theme_text_color: "Custom" # 设置输入文本颜色为黑色 text_color: 0, 0, 0, 1 # 设置提示文本颜色为黑色 hint_text_color: 0, 0, 0, 1 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" # 开启自定义文本颜色模式 theme_text_color: "Custom" # 设置输入文本颜色为黑色 text_color: 0, 0, 0, 1 # 设置提示文本颜色为黑色 hint_text_color: 0, 0, 0, 1 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()
额外说明
- 若需要修改辅助文本(helper_text)颜色,可添加
helper_text_color: 0, 0, 0, 1属性; - 如需根据背景色动态切换文本颜色,可在Python代码中监听背景色变化事件,实时修改
text_color和hint_text_color的值。
内容的提问来源于stack exchange,提问作者RAHRAH
相关产品推荐
相关产品推荐

