kivy: “Invalid property name” error message for a

2019-08-19 03:27发布

问题:

If I put this into the main program:

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

And this into the kv file:

#: import MyTextInput __main__.MyTextInput

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

Then the behaviour is correct, this is printed whenever the text input gets or looses the focus:

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

However, this does not work at all:

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

Kivy refuses to compile the .kv file with this message:

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

Why? Is this a bug?

UPDATE: Changed the title so that others can find this easily (as it turned out, it has nothing to do with that particular property name).

回答1:

You have a syntax error, try this:

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