简体   繁体   English

问:Kivy 无效的类名

[英]Q: Kivy Invalid Class Name

I have started learning Kivy framework by reading "Creating Apps in Kivy" by Dusty Phillips.我通过阅读 Dusty Phillips 的“Creating Apps in Kivy”开始学习 Kivy 框架。 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".我已经按照书中所说的做了所有事情,我以为我也明白我在做什么,但后来我遇到了“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.添加WeatherRoot: root 小部件和<WeatherRoot>:类规则后,代码中断了。 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

You cannot have a class rule inside another class rule.您不能在另一个班级规则中包含一个班级规则。 The solution is one of the following:解决方案是以下之一:

  • Remove class rule, <AddLocationForm>:删除类规则, <AddLocationForm>:
  • Fix the indentation for class rule, <AddLocationForm>:修复类规则的缩进, <AddLocationForm>:
  • Check that there is class defined for AddLocationForm in your Python code.检查 Python 代码中是否为AddLocationForm定义了类。

Note笔记

Avoid declaring both root rule, WeatherRoot: and class rule, <WeatherRoot>: in the kv file to avoid confusion.避免在 kv 文件中同时声明根规则WeatherRoot:和类规则<WeatherRoot>:以避免混淆。

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测试.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输出

图像01

I have started learning Kivy framework by reading "Creating Apps in Kivy" by Dusty Phillips.通过阅读Dusty Phillips的“在Kivy中创建应用程序”,我开始学习Kivy框架。 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".我已经按照书中的说明完成了所有工作,我以为自己也了解自己在做什么,但是后来遇到了“ 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.添加WeatherRoot:根小部件和<WeatherRoot>:类规则后,代码中断。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM