Python在分配用户定义的Kivy小部件属性时报告无效语法

2024-10-04 11:33:37 发布

您现在位置:Python中文网/ 问答频道 /正文

from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.base import runTouchApp 
from kivy.properties import StringProperty
from kivy.properties import ListProperty
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color

Builder.load_string('''
<bbx>:
    orientation: 'vertical'
    my2App:
        color: 1,0,0,1

<my2App>:
    text: root.text
    Label:
        text: root.text
        font_size: 16
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
        canvas:
            Color:
                rgba: root.color
            Rectangle:
                pos: self.pos
                size: self.size
''')

class my2App(ScrollView):
    text = StringProperty('default string'*200)
    color = ListProperty([1,0,0,0.25])

class bbx(BoxLayout):
    pass

runTouchApp(bbx())

这是我练习的kivy代码。my2App是一个用户定义的小部件,主要是从本教程(https://www.youtube.com/watch?v=WdcUg_rX2fM)中复制的。唯一添加的是ListProperty定义的color属性。不知怎的,这个用户定义的颜色属性没有成功。我试着运行my2App,但也没用。在

^{pr2}$

Tags: textfromimportselfsize定义rootlabel