KIVY python - 更改 TextInput 颜色
KIVY python - change TextInput color
我想在用户修改 TextInput 时更改包含的文本的颜色。
示例:
- 有文字输入,里面有'pre-written'文字
- 如果您修改此文本的一个字符,它的颜色会立即变为红色
debug2.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class MyDebug(BoxLayout):
def __init__(self, **kwargs):
super(MyDebug, self).__init__(**kwargs)
Builder.load_file("debug2.kv")
class MyAppli(App):
def build(self):
return MyDebug()
if __name__ == '__main__':
MyAppli().run()
debug2.kv
#:kivy 1.9.1
<MyDebug>:
TextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
foreground_color: (0.3, 0.4, 0.5, 1)
给你
py:
class ColorTextInput(TextInput):
def changetored(self):
if self.text != "Edit me !":
self.foreground_color = (1,0,0,1)
kv:
ColorTextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
on_text: self.changetored()
您将一个方法添加到您的根小部件 MyDebug 和一个事件,on_text 到您的 kv 文件中,如下所示:
片段
debug2.py
class MyDebug(BoxLayout):
def __init__(self, **kwargs):
super(MyDebug, self).__init__(**kwargs)
def on_text_change(self, instance, value):
instance.foreground_color = (1, 0, 0, 1)
debug2.kv
<MyDebug>:
TextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
foreground_color: (0.3, 0.4, 0.5, 1)
on_text: root.on_text_change(self, self.text)
输出
更改文本颜色的最简单方法是使用 on_text
TextInput:
text: 'Color will change from black to red if text is modified'
on_text: self.foreground_color = (1,0,0,1)##red
foreground_color = (0,0,0,1)
我想在用户修改 TextInput 时更改包含的文本的颜色。 示例:
- 有文字输入,里面有'pre-written'文字
- 如果您修改此文本的一个字符,它的颜色会立即变为红色
debug2.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class MyDebug(BoxLayout):
def __init__(self, **kwargs):
super(MyDebug, self).__init__(**kwargs)
Builder.load_file("debug2.kv")
class MyAppli(App):
def build(self):
return MyDebug()
if __name__ == '__main__':
MyAppli().run()
debug2.kv
#:kivy 1.9.1
<MyDebug>:
TextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
foreground_color: (0.3, 0.4, 0.5, 1)
给你
py:
class ColorTextInput(TextInput):
def changetored(self):
if self.text != "Edit me !":
self.foreground_color = (1,0,0,1)
kv:
ColorTextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
on_text: self.changetored()
您将一个方法添加到您的根小部件 MyDebug 和一个事件,on_text 到您的 kv 文件中,如下所示:
片段
debug2.py
class MyDebug(BoxLayout):
def __init__(self, **kwargs):
super(MyDebug, self).__init__(**kwargs)
def on_text_change(self, instance, value):
instance.foreground_color = (1, 0, 0, 1)
debug2.kv
<MyDebug>:
TextInput:
text: "Edit me !" # change color to (1, 0, 0, 1) when modified
foreground_color: (0.3, 0.4, 0.5, 1)
on_text: root.on_text_change(self, self.text)
输出
更改文本颜色的最简单方法是使用 on_text
TextInput:
text: 'Color will change from black to red if text is modified'
on_text: self.foreground_color = (1,0,0,1)##red
foreground_color = (0,0,0,1)