【问题标题】:kivy: "Invalid property name" error message for a valid property namekivy:有效属性名称的“无效属性名称”错误消息
【发布时间】:2017-11-11 10:32:39
【问题描述】:

如果我把它放到主程序中:

class MyTextInput(TextInput):
    def on_focus(self, *args, **kwargs):
        print("Yay!", args, kwargs)

并将其放入 kv 文件中:

#: import MyTextInput __main__.MyTextInput

                MyTextInput:
                    id: e_birth_date
                    text: ""
                    size_hint_x: 1

那么行为是正确的,每当文本输入获得或失去焦点时都会打印出来:

Yay! (<__main__.MyTextInput object at 0x0CC1B8B8>, True) {} 
Yay! (<__main__.MyTextInput object at 0x0CC1B8B8>, False) {}

但是,这根本不起作用:

                TextInput:
                    id: e_birth_date
                    text: ""
                    size_hint_x: 1
                    on_focus = root.on_field_focus(*args)

Kivy 拒绝使用此消息编译 .kv 文件:

kivy.lang.parser.ParserException: Parser: File "C:\not_telling\app.kv", line 185:
 ...
     183:                        text: ""
     184:                        size_hint_x: 1
 >>  185:                        on_focus = root.on_field_focus(*args)
     186:                    TextInput:
     187:                        id: e_phone
 ...
 Invalid property name

为什么?这是一个错误吗?

更新:更改了标题,以便其他人可以轻松找到它(事实证明,它与该特定属性名称无关)。

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    你有语法错误,试试这个:

    TextInput:
        id: e_birth_date
        text: ""
        size_hint_x: 1
        on_focus: root.on_field_focus(*args)
    

    【讨论】:

    • 我的错。我会立即删除我的问题,但出于一个原因我不会。该消息显示“无效的属性名称”而不是“语法错误”。非常混乱!
    • 我猜冒号之前的所有东西都被认为是类名或属性
    • 如果编译器没有看到冒号,他会给出 Invalid Property Name 消息
    • 但这很糟糕。事实上,我从未见过任何非注释行不包含冒号的 kv 文件。应该是语法错误,而不是“无效的属性名”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2021-10-07
    相关资源
    最近更新 更多