问:Kivy无效的类名

时间:2018-09-13 16:09:56

标签: python kivy

我已经开始阅读Dusty Phillips的“在Kivy中创建应用程序”来学习Kivy框架。我已经按照书中的说明进行了所有操作,我以为自己也了解自己在做什么,但是后来遇到了“ ParserException”。

这是我的代码:

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: []

添加了WeatherRoot:根窗口小部件和<WeatherRoot>:类规则后,代码中断了。在添加这些代码之前,这些代码可以正常工作。

这是我得到的错误:

 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

2 个答案:

答案 0 :(得分:1)

您不能在另一个类规则中包含一个类规则。解决方案是以下之一:

  • 删除班级规则<AddLocationForm>:
  • 修复类规则<AddLocationForm>:的缩进
  • 检查Python代码中是否为 AddLocationForm 定义了类。

注意

避免在kv文件中同时声明根规则WeatherRoot:和类规则<WeatherRoot>:,以避免混淆。

摘要

<WeatherRoot>:
    AddLocationForm:

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

示例

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: []

输出

Img01

答案 1 :(得分:0)

以下是kv文件的代码:

通过阅读Dusty Phillips的“在Kivy中创建应用程序”,我开始学习Kivy框架。我已经按照书中的说明完成了所有工作,而且我以为自己也了解自己在做什么,但是后来遇到了“ ParserException”。

这是我的代码:

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: []