首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >问: Kivy无效类名

问: Kivy无效类名
EN

Stack Overflow用户
提问于 2018-09-13 16:09:56
回答 1查看 1.8K关注 0票数 0

我已经开始学习基维框架,阅读杜斯蒂菲利普斯的“在基维中创建应用程序”。我已经做了所有的事情,它在书中说,我认为我也是理解我在做什么,但后来我遇到了一个"ParserException“。

这是我的密码:

代码语言:javascript
复制
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>:类规则之后,代码崩溃了。在添加这些代码之前,代码工作得很好。

下面是我遇到的错误:

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-13 16:17:37

不能在另一个类规则中有类规则。解决方案如下:

  • 删除类规则,<AddLocationForm>:
  • 修正类规则的缩进,<AddLocationForm>:
  • 检查在您的Python代码中是否有为AddLocationForm定义的类。

备注

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

片段

代码语言:javascript
复制
<WeatherRoot>:
    AddLocationForm:

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

示例

main.py

代码语言:javascript
复制
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

代码语言:javascript
复制
#: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: []

输出

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52317725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档