簡體   English   中英

問:Kivy 無效的類名

[英]Q: Kivy Invalid Class Name

我通過閱讀 Dusty Phillips 的“Creating Apps in 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: root 小部件和<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

您不能在另一個班級規則中包含一個班級規則。 解決方案是以下之一:

  • 刪除類規則, <AddLocationForm>:
  • 修復類規則的縮進, <AddLocationForm>:
  • 檢查 Python 代碼中是否為AddLocationForm定義了類。

筆記

避免在 kv 文件中同時聲明根規則WeatherRoot:和類規則<WeatherRoot>:以避免混淆。

片段

<WeatherRoot>:
    AddLocationForm:

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

例子

主文件

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()

測試.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: []

輸出

圖像01

通過閱讀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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM