slinden
slinden

Reputation: 1025

Q: Kivy Invalid Class Name

I have started learning Kivy framework by reading "Creating Apps in Kivy" by Dusty Phillips. I have done everything as it says in the book and I thought I was also understanding what I was doing, but then I encountered a "ParserException".

This is my code:

WeatherRoot:

<WeatherRoot>:
    AddLocationForm:

    <AddLocationForm>:
        orientation: "vertical"
        # Set a value for the property that was created in the .py file.
        search_input: search_box
        search_results: search_results_list
        BoxLayout:
            height: "40dp"
            size_hint_y: None
            TextInput:
                # Define an id for the widget so that it can be referenced
                # from elsewhere in the KV file
                id: search_box
                size_hint_x: 50
                multiline: False
                # on_text_validate: root.search_location()
            Button:
                text: "Search"
                size_hint_x: 25
                on_press: root.search_location()
            Button:
                text: "Current Location"
                size_hint_x: 25
                on_press: root.search_location_by_coordinates()

        ListView:
            id: search_results_list
            item_strings: []

After adding WeatherRoot: root widget and <WeatherRoot>: class rule the code broke. Before adding those the code worked just fine.

Here is the error I get:

 kivy.lang.parser.ParserException: Parser: File "c:\Users\Utente- 
 006\Dropbox\Programming\rss-reader\weather.kv", line 8:
 ...
   6:    AddLocationForm:
   7:    
  > 8:   <AddLocationForm>:
   9:        orientation: "vertical"
  10:        # Set a value for the property that was created in the .py file.
 ...
 Invalid class name

Upvotes: 0

Views: 2275

Answers (1)

ikolim
ikolim

Reputation: 16011

You cannot have a class rule inside another class rule. The solution is one of the following:

  • Remove class rule, <AddLocationForm>:
  • Fix the indentation for class rule, <AddLocationForm>:
  • Check that there is class defined for AddLocationForm in your Python code.

Note

Avoid declaring both root rule, WeatherRoot: and class rule, <WeatherRoot>: in the kv file to avoid confusion.

Snippet

<WeatherRoot>:
    AddLocationForm:

<AddLocationForm>:
    orientation: "vertical"
    ...

Example

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout


class WeatherRoot(Screen):
    pass


class AddLocationForm(BoxLayout):
    pass


class Test(App):

    def build(self):
        return WeatherRoot()


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

test.kv

#:kivy 1.11.0

<WeatherRoot>:
    AddLocationForm:

<AddLocationForm>:
    orientation: "vertical"
    # Set a value for the property that was created in the .py file.
    search_input: search_box
    search_results: search_results_list

    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            # Define an id for the widget so that it can be referenced
            # from elsewhere in the KV file
            id: search_box
            size_hint_x: 50
            multiline: False
            # on_text_validate: root.search_location()

        Button:
            text: "Search"
            size_hint_x: 25
            on_press: root.search_location()

        Button:
            text: "Current Location"
            size_hint_x: 25
            on_press: root.search_location_by_coordinates()

    ListView:
        id: search_results_list
        item_strings: []

Output

Img01

Upvotes: 2

Related Questions