0
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())

This is my practice kivy code. my2App is a user defined widget mostly copied from this tutorial (https://www.youtube.com/watch?v=WdcUg_rX2fM). The only addition is the color property defined by ListProperty. Somehow this user defined color property didn't work out. I tried to run my2App along and it didn't work either.

> Traceback (most recent call last):    File "test_anotherviky.py", line
> 38, in <module>
>      ''')    File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1796, in load_string
>      parser = Parser(content=string, filename=fn)    File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1185, in
> __init__
>      self.parse(content)    File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1291, in parse
>      rule.precompile()    File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1049, in
> precompile
>      x.precompile()    File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 976, in
> precompile
>      self.co_value = compile(value, self.ctx.filename or '<string>', mode)    File "<string>", line 5
>      color: 1,0,0,1
>           ^  SyntaxError: invalid syntax
1
  • What is the full traceback? Commented Jul 13, 2015 at 6:00

2 Answers 2

1

Widget names must start with an upper case letter to work in kv, as it uses this to distinguish them from properties. Here, it thinks my2app: is a property setting.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It works now. But I still cannot set the property value in <Bbx>. Do I have to do this using a method?
@DaveQ how were you able to set the ListProperty? I am facing issues with code similar to yours, where if I try to set the values in a listproperty of strings from the kv file, the list is actually empty in my widget's instances
1

I'm using PyCharm 2020.1 and Kivy 1.10.1 and I was having the same problem: color: 1,0,0,1

^ SyntaxError: invalid syntax But in my case was with: size_hint: None, 1. I fix it using camelcase. Like this:

.py file: class BoxBlue(BoxLayout): None

.kv file: BoxBlue: size_hint: None,1 width: 30

My class was named: boxBlue, so I changed to BoxBlue in py and kv, and works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.