kivy TextInput 更改字体颜色添加背景线
kivy TextInput change font color add background lines
我正在开发一个简单的笔记应用程序。这就是我添加注释的屏幕现在的样子。
我想实现两件事:
1.将我的 TextInput 的字体颜色更改为白色。
2。如图所示向我的 TextInput 添加行
即使 TextInput 为空,线条也应始终可见。
从我的 python 脚本中提取:
class NewNoteView(ModalView):
pass
从我的 kv 文件中提取
<NewNoteView>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgb: 33/255, 41/255, 55/255
Rectangle:
pos: self.pos
size: self.size
orientation: 'vertical'
BoxLayout:
Button:
size_hint_x:0.1
text: 'x'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
on_press: root.dismiss()
Label:
text: 'New Label'
Button:
size_hint_x:0.1
text:'+'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
on_press: app.root.notifyP()
Button:
size_hint_x:0.1
text: 'L'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
BoxLayout:
TextInput:
font_size: self.height*0.5
background_color: 0, 0, 0, 0
cursor_color: 1, 1, 1, 1
hint_text: 'Heading'
multiline: False
padding_x: [30,30]
Button:
size_hint_x: 0.2
BoxLayout:
canvas.before:
Color:
rgba: [38/255, 49/255, 70/255,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
background_color: 0, 0, 0, 0
cursor_color: 1, 1, 1, 1
color: 1, 1, 1, 1
hint_text: 'Body'
padding_x: [30,30]
您的问题结构非常清晰(谢谢!)但是您没有真正正确地进行研究,是吗?至少关于第一个问题。
- 答案很简单:使用foreground_color属性并将其设置为
1, 1, 1, 1
;来自文档:
(fore_ground color is the) current color of the foreground, in (r, g,
b, a) format. Defaults to black.
- 现在这个更有趣也更复杂了。直接的解决方案是在 canvas 中使用
Line
。像这样的东西会在 .kv 文件中的小部件底部添加一条白线:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Line:
points: self.x + 20, self.y, self.x + self.width - 20, self.y
width: 1
当我将您的应用更改为使用两个 TextInput 时,它看起来像这样:
但是,据我了解,您只想在一张便条中使用一个 TextInput。所以你必须计算出线高并每隔 x 像素画一条线。 Here's 我发现了什么。您需要使用 minimum_height 或 line_height 加上 line_spacing。另外,我认为您不能在 .kv 中执行此操作,我认为您需要在 Python.
中为其编写一个方法
我建议将我的方法与额外的 TextInputs 一起使用。您可以每次绑定 "enter" 以创建一个新的 TextInput,这样您就可以拥有无限行。我相信这会更容易,但您可以采用任何一种方式。
我正在开发一个简单的笔记应用程序。这就是我添加注释的屏幕现在的样子。
我想实现两件事:
1.将我的 TextInput 的字体颜色更改为白色。
2。如图所示向我的 TextInput 添加行
即使 TextInput 为空,线条也应始终可见。
从我的 python 脚本中提取:
class NewNoteView(ModalView):
pass
从我的 kv 文件中提取
<NewNoteView>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: 0.2
canvas.before:
Color:
rgb: 33/255, 41/255, 55/255
Rectangle:
pos: self.pos
size: self.size
orientation: 'vertical'
BoxLayout:
Button:
size_hint_x:0.1
text: 'x'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
on_press: root.dismiss()
Label:
text: 'New Label'
Button:
size_hint_x:0.1
text:'+'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
on_press: app.root.notifyP()
Button:
size_hint_x:0.1
text: 'L'
font_size: self.height*0.5
background_color: 0, 0, 0, 0
BoxLayout:
TextInput:
font_size: self.height*0.5
background_color: 0, 0, 0, 0
cursor_color: 1, 1, 1, 1
hint_text: 'Heading'
multiline: False
padding_x: [30,30]
Button:
size_hint_x: 0.2
BoxLayout:
canvas.before:
Color:
rgba: [38/255, 49/255, 70/255,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
background_color: 0, 0, 0, 0
cursor_color: 1, 1, 1, 1
color: 1, 1, 1, 1
hint_text: 'Body'
padding_x: [30,30]
您的问题结构非常清晰(谢谢!)但是您没有真正正确地进行研究,是吗?至少关于第一个问题。
- 答案很简单:使用foreground_color属性并将其设置为
1, 1, 1, 1
;来自文档:
(fore_ground color is the) current color of the foreground, in (r, g, b, a) format. Defaults to black.
- 现在这个更有趣也更复杂了。直接的解决方案是在 canvas 中使用
Line
。像这样的东西会在 .kv 文件中的小部件底部添加一条白线:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Line:
points: self.x + 20, self.y, self.x + self.width - 20, self.y
width: 1
当我将您的应用更改为使用两个 TextInput 时,它看起来像这样:
但是,据我了解,您只想在一张便条中使用一个 TextInput。所以你必须计算出线高并每隔 x 像素画一条线。 Here's 我发现了什么。您需要使用 minimum_height 或 line_height 加上 line_spacing。另外,我认为您不能在 .kv 中执行此操作,我认为您需要在 Python.
中为其编写一个方法我建议将我的方法与额外的 TextInputs 一起使用。您可以每次绑定 "enter" 以创建一个新的 TextInput,这样您就可以拥有无限行。我相信这会更容易,但您可以采用任何一种方式。