Search code examples
pythonpython-3.xvisual-studio-codekivykivy-language

Visual Studio Python Kivy Unknown Directive Error


I'm currently trying to run a basic kivy script in Visual Studio Code, but everytime I run the .kv file, I get an 'Unknown directive' error in the terminal. I have installed the Kivy Extension by Battle Bas and installed kivy on my device via the kivy website, however the error still persists.

This is My Python Script:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget

class MyGrid(Widget):
    pass


class MyApp(App):
    def build(self):
        return MyGrid()


if __name__=="__main__":
    MyApp().run()

And This is my Kivy Script in the .kv file:

#:kivy2.0.0
<MyGrid>
   Label:
       text: "Hello!"

This is What it Says in My Terminal:

     raise ParserException(self, ln, 'Unknown directive')
 kivy.lang.parser.ParserException: Parser: File "c:\Users\hasan\kivy-Tutorial ver2\my.kv", line 1:
 ...
 >>    1:#:kivy2.0.0
       2:<MyGrid>
       3:    Label:
 ...
 Unknown directive

I would really appreciate it if someone would help because this is my first time using Stack Overflow and I really want to build apps with Python. Thank You!


Solution

  • According to the documentation:

    The content of the file should always start with the Kivy header, where version must be replaced with the Kivy language version you’re using. For now, use 1.0:

    #:kivy 1.0

    Note that the header is #:kivy followed by a space then the string version.

    Try changing your header from:

    #:kivy2.0.0
    

    to

    #:kivy 2.0.0