python - kivy TextInput更改字体颜色添加背景线

标签 python kivy kivy-language

我正在开发一个简单的笔记应用程序。这就是我添加注释的屏幕现在的样子。 ![enter image description here

我想实现两件事:

<强>1。将我的 TextInput 的字体颜色更改为白色。

<强>2。将线条添加到我的 TextInput,如图所示

enter image description here

即使 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]

最佳答案

您的问题结构非常清晰(谢谢!),但您并没有真正正确地进行研究,不是吗?至少对于第一个问题。

  1. 答案很简单:使用 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: 颜色: RGBA: 1, 1, 1, 1 线: 点:self.x + 20,self.y,self.x + self.width - 20,self.y 宽度:1

    当我将您的应用程序更改为使用两个 TextInput 时,情况如下所示: How it looks

    但是,据我了解,您只想在注释中拥有一个 TextInput。所以你必须计算出线条高度并每 x 像素画一条线。 Here's我发现了什么。您需要使用minimum_height或line_height加上line_spacing。另外,我认为您不能在 .kv 中执行此操作,我认为您需要在 Python 中为其编写一个方法。

    我建议将我的方法与额外的 TextInputs 一起使用。您可以绑定(bind)“enter”每次创建一个新的 TextInput,这样您就可以拥有无​​限行。我相信这会更容易,但您可以采用任何一种方法。

    关于python - kivy TextInput更改字体颜色添加背景线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181992/

    相关文章:

    python - 为什么对稀疏矩阵求和会留下空维度?

    python - Kivy 文本标记打印自己的语法

    python - 如何在 Kivy 中将标签位置设置为边框?

    python - html 中的 CherryPy 变量

    python - 如何提取具有最大值的 Django 对象?

    python - Hadoop与Hadoop上的图形路径

    java - Android 上的 Kivy,在空对象引用上发生 JVM 异常 : Attempt to invoke virtual method . ..

    python - 单击按钮时 Kivy 屏幕不发生变化 python 代码

    android - 为什么buildozer显示错误: Activity class {org. test.myapp/org.renpy.android.PythonActivity}不存在

    python - ScrollView 在 kivy 中显示不同的视频一段时间后会滞后