Problem with basics of Kivy (prolly .kv file)

Question:

Can you guys have a look at my python/kivy code?

main.py (copied from video tutorial):

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

class MainWidget(Widget):
    pass

class TheLabApp(App):
    pass

TheLabApp().run()

thelab.kv:

#:kivy ! 2.1.0

MainWidget:

<MainWidget>:
    Button:
        text: "Hello"
        size: "200dp", "100dp"
        pos: "100dp", "200dp"

Running this code ends up with diffrent results. Sometimes it compiles returning empty window, sometimes it ends up with this (or similar) errors:

 Traceback (most recent call last):
   File "c:UsersklaudDocumentsStudiaA Pythontestmain.py", line 10, in <module>
     TheLabApp().run()
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivyapp.py", line 954, in run
     self._run_prepare()
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivyapp.py", line 923, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivyapp.py", line 696, in load_kv
     root = Builder.load_file(rfilename)
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivylangbuilder.py", line 305, in load_file
     return self.load_string(data, **kwargs)
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivylangbuilder.py", line 372, in load_string
     parser = Parser(content=string, filename=fn)
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivylangparser.py", line 483, in __init__
     self.parse(content)
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivylangparser.py", line 590, in parse
     self.execute_directives()
   File "C:UsersklaudAppDataRoamingPythonPython310site-packageskivylangparser.py", line 569, in execute_directives
     raise ParserException(self, ln, 'Unknown directive')
 kivy.lang.parser.ParserException: Parser: File "c:UsersklaudDocumentsStudiaA Pythontestthelab.kv", line 1:
 ...
 >>    1:#:kivy
       2:
       3:MainWidget:
 ...
 Unknown directive

Code is copied from yt, where it worked just fine. Have you got any ideas?

Asked By: DR4NKR1D3R

||

Answers:

According to the documentation, the first line of a .kv file should be

#:kivy `1.0`

where 1.0 is the lowest version you support, or simply the version you’re currently using. I have version 2.1.0, so my header line is

#:kivy 2.1.0

without the !. That worked fine for me.

Completely removing the header also worked.

Answered By: MattDMo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.