2017-11-11 236 views
0

如果我把這個到主程序中的錯誤信息:kivy:「無效的屬性名稱」爲有效的屬性名稱

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 

爲什麼?這是一個錯誤?

UPDATE:改變了標題,以便其他人可以輕鬆地找到它(事實證明,它與該特定屬性名稱無關)。

回答

1

你有一個語法錯誤,試試這個:

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

我的錯。我會立即刪除我的問題,但我不會因爲一個原因。該消息說「無效的屬性名稱」,而不是「語法錯誤」。很混亂! – nagylzs

+0

我想冒號前的所有內容都被認爲是類名或屬性 –

+0

,如果編譯器沒有看到冒號,他會給出無效的屬性名稱消息 –