Getting SyntaxError: invalid syntax from kv file

1,459 views
Skip to first unread message

Jacek Nowak

unread,
Jan 30, 2014, 10:47:18 AM1/30/14
to kivy-...@googlegroups.com
I'm using kivy 1.7.2 on Ubuntu 12.04 LTS.

I'm trying to run a very simple program and getting errors. I can't figure out what I'm doing wrong here.

main.py:

from kivy.app import App
from kivy.uix.widget import Widget

class kvtest(Widget):
   
pass
   
class myWidget(Widget):
   
pass
   
class kvtestApp(App):
   
def build(self):
       
return kvtest()

if __name__ == '__main__':
    kvtestApp
().run()


kvtest.kv:

#:kivy 1.7.2

<myWidget>:
    size_hint
: None, None
    canvas
.after:
       
Color:
            rgba
: 1, 0, 0, .5
       
Rectangle:
            size
: self.size
            pos
: self.pos
   
<kvtest>:
   
AnchorLayout:
        size_hint
: None, None
        size
: self.parent.size
        canvas
.after:
           
Color:
                rgba
: 0, 1, 0, .1
           
Rectangle:
                size
: self.size
                pos
: self.pos

        myWidget
:
            size
: min(root.width , root.height) / 12, min(root.width, root.height) / 12


When I try to run this code, I get the following output:


 
Traceback (most recent call last):
   
File "main.py", line 15, in <module>
     kvtestApp
().run()
   
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 576, in run
     
self.load_kv(filename=self.options.get('kv_file'))
   
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 399, in load_kv
     root
= Builder.load_file(filename)
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1323, in load_file
     
return self.load_string(data, **kwargs)
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1360, in load_string
     parser
= Parser(content=string, filename=fn)
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 930, in __init__
     
self.parse(content)
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1007, in parse
     rule
.precompile()
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 802, in precompile
     x
.precompile()
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 798, in precompile
     x
.precompile()
   
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 733, in precompile
     
self.co_value = compile(value, self.ctx.filename or '<string>', mode)
   
File "./kvtest.kv", line 1
     size
: min(root.width , root.height) / 12, min(root.width, root.height) / 12
         
^
 
SyntaxError: invalid syntax



Any suggestions?

Jacek Nowak

unread,
Jan 30, 2014, 10:56:55 AM1/30/14
to kivy-...@googlegroups.com
I forgot to add that changing myWidget to Widget in the .kv file makes the example work fine. The syntax error is not caused by the syntax of the "size" assignment - whatever I put there I receive the same error on the first line in the myWidget instance. It's like the builder does not understand myWidget class somehow, but I believe that it should be correct - Pong tutorial has similar declarations...

Alexander Taylor

unread,
Jan 30, 2014, 11:02:46 AM1/30/14
to kivy-...@googlegroups.com
It seems like the problem is to do with capitalisation, if I replace myWidget with MyWidget it works fine.

Capitalised class names would be a normal python convention, but I didn't realise kv required it.

Ben Rousch

unread,
Jan 30, 2014, 11:08:14 AM1/30/14
to kivy-...@googlegroups.com
PEP8 all the things!


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
 Ben Rousch
   bro...@gmail.com
   http://clusterbleep.net/

Brendon Higgins

unread,
Jan 30, 2014, 11:20:11 AM1/30/14
to kivy-...@googlegroups.com
Shouldn't it be more like "pep8 all the Things", then? ;-)

Jacek Nowak

unread,
Jan 31, 2014, 1:38:32 AM1/31/14
to kivy-...@googlegroups.com
Wow, that was fast! Thanks a lot, I wasn't able to find it for two hours. I guess the reason for this would be that kivy has no other way to find out if I mean a property or a child widget. It has to use the case of the first letter of the name to decide.
Reply all
Reply to author
Forward
0 new messages