Search code examples
pythonkivykivy-language

Error while builder loading in kivy: Invalid indentation, must be a multiple of "n" spaces


So I've been working on a kivy app and was having some trouble with Builder loading some kv language, the code crashes when I run it.

Error message:

   File "C:\Users\areef\PycharmProjects\pythonProject1\venv\venev2\lib\site-packages\kivy\lang\parser.py", line 566, in parse_level
     raise ParserException(self, ln,
 kivy.lang.parser.ParserException: Parser: File "<inline>", line 3:
 ...
       1:Screen:
       2:        FloatLayout:
 >>    3:            Button:
       4:                text: "It works!"
       5:                pos_hint: {"x": 0.1, "y": 0.7}
 ...
 Invalid indentation, must be a multiple of 8 spaces

Code:

from kivy.app import App
from kivy.lang import Builder

kv = """Screen:
        FloatLayout:
            Button:
                text: "It works!"
                pos_hint: {"x": 0.1, "y": 0.7}
                text_size: self.size
        """


class MyApp(App):
    def build(self):
        self.screen = Builder.load_string(kv)
        return self.screen


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

What am I doing wrong, can someone help me to fix this error?


Solution

  • When you use a triple quoted string, the leading spaces of each line are part of the string. The first indentation in your cube string is 20 spaces, so all the indentation is expected to be multiples of that. To fix that problem, just eliminate all those extra spaces. Try something like:

            cube = """
    Label:
        text:"Side:"
        pos_hint: {"x":1.1, "y":0.7}
        text_size: self.size
    Label:
        text:"Volume:"     
        .
        .
        .
               
    

    Where the Labels are at zero indentation. That will solve the error you are seeing, and another error (unrelated to formatting) will appear.