Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy文本标记打印自己的语法_Python_Kivy_Kivy Language - Fatal编程技术网

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

Python Kivy文本标记打印自己的语法,python,kivy,kivy-language,Python,Kivy,Kivy Language,我在测试Kivy的标记功能。我的测试程序的基本轮廓是有4个标签和一个按钮,如果按下按钮,它会改变标签文本第一个字母的颜色。现在的问题是,当我第一次按下按钮时,它会更改所有标签文本的第一个字母的颜色,但从第二次按下开始,它会以相反的方式在文本开头添加标记语法。以下是节目: from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button from kivy.uix

我在测试Kivy的标记功能。我的测试程序的基本轮廓是有4个标签和一个按钮,如果按下按钮,它会改变标签文本第一个字母的颜色。现在的问题是,当我第一次按下按钮时,它会更改所有标签文本的第一个字母的颜色,但从第二次按下开始,它会以相反的方式在文本开头添加标记语法。以下是节目:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder
import string

Builder.load_string(
'''
<CLabel@Label>:
    markup: True

<box>:
    orientation: 'vertical'

    Button:
        text: 'press'
        on_press: app.change()


    CLabel:
        id: a
        text: 'abcd'

    CLabel:
        id: b
        text: 'efgh'

    CLabel:
        id: c
        text: 'ijkl'

    CLabel:
        id: d
        text: 'mnop'
'''
)

class box(BoxLayout):
    pass

class main(App):
    def change(self):
        for lol in string.lowercase[:4]:
            self.root.ids[lol].text = '[color=#E5D209]{}[/color]{}'.format(self.root.ids[lol].text[0], self.root.ids[lol].text[1:])

    def build(self):
        return box()

if __name__ == "__main__":
    main().run()
它工作得非常好。但是通过这种方法,我将不得不复制粘贴很多行。这只是我工作的一小部分。我正在做的真正的项目有超过15个标签,每个标签的复制粘贴都很烦人。如果是循环的话会更好。它使工作简短而精确

在此之后,出于沮丧,我尝试了以下代码使用get_color_from_hex方法:

self.root.ids[lol].text[0] = self.root.ids[lol].text[0].get_color_from_hex('#E5D209')
但我最终收到一条错误消息,上面说:

AttributeError: 'str' object has no attribute 'color'

如果有人能想出办法来改变《天知道有多少个标签》文本第一个字母的颜色,我会非常高兴的。:'(

标记是存储在
text
中的字符串的一部分。因此,第二次运行循环时,第一个字符(
[
)确实插入到标记标记之间,扰乱了解析过程

您可以通过将原始文本存储在另一个
StringProperty
,我们称之为
\u hidden\u text
中来实现

self.root.ids[lol].text = '[color=#E5D209]{}[/color]{}'.format(self.root.ids[lol]._hidden_text[0], self.root.ids[lol]._hidden_text[1:])
这样可以避免重用添加的标记。 当然,您可能需要设置绑定以使赋值
\u隐藏\u文本
→<代码>文本自动

编辑: 添加此类定义:

class CLabel(Label):
    hidden_text = StringProperty('')
然后将
CLabel
的kv样式更改为

<CLabel>:
    markup: True
    text: self.hidden_text

我不想这么说,但我在使用属性方面没有太多经验。而且,当我使用属性时,我遇到了远远超出我理解范围的问题。\但是我找到了一种不使用StringProperty的程序解决方案。这里,看一看:现在,问题是我必须在标签下键入两次文本。\当我尝试
hidden\u text:self.text
,我遇到了与以前相同的问题。即使我将
hidden\u text
分配给
self.root.ids[lol].text
在for循环下,我遇到了相同的问题。我尝试在此处使用StringProperty。这是使用StringProperty的方法吗?但输出仍然存在问题中提到的问题。@PartHibbis添加了如何使用
StringProperty
的示例(它应该是
CLabel
类的属性),并演示了如何不必双重键入文本。顺便说一句,如果您需要
CLabel
类的行为类似于
Label
(即,文本应通过
text
进行设置),请看一看
AliasProperty
。它成功了!我肯定会研究AliasProperty。在这样做的同时,我还必须开始更多地练习其他属性。谢谢。
<CLabel>:
    markup: True
    text: self.hidden_text
CLabel:
    id: a
    hidden_text: 'abcd'