Kivy v1.10.0을 테스트 중이며 Kivy 속성을 설정 한 위치가 왜 다른지 이해하지 못합니다.bind()를 사용할 때 init()에서 선언 된 Kivy 속성에서 오류가 발생하는 이유는 무엇입니까?
이 코드는 작동합니다
from kivy.app import App
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
class CustomBtn(Widget):
pressed = ListProperty([0, 0])
def __init__(self, **kwargs):
super(CustomBtn, self).__init__(**kwargs)
# self.pressed = ListProperty([0, 0])
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.pressed = touch.pos
return True
return super(CustomBtn, self).on_touch_down(touch)
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
cb = CustomBtn()
self.add_widget(cb)
cb.bind(pressed=self.btn_pressed)
def btn_pressed(self, instance, pos):
print(pos)
class MyApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
MyApp().run()
을하지만, 내가 클래스 수준에서 현재 라인을 교체하는 경우 : CustomBtn.__init__()
에 해당 의해
pressed = ListProperty([0, 0])
:
self.pressed = ListProperty([0, 0])
내가 얻을 안내의 오류 cb.bind(pressed=self.btn_pressed)
:
File "kivy\_event.pyx", line 438, in kivy._event.EventDispatcher.bind (kivy\_event.c:6500)
KeyError: 'pressed'
어떤 방법 으로든 클래스 수준에서 속성을 선언 (할당)하고 동일하게 수행하는 것은 동일하다고 생각합니다. Kivy 속성은 Python 속성이 아니며, 객체가 작성되는 순서가 달라서 Kivy에 차이가 생길 수 있습니까? 내가 어떤 방법 중 클래스 수준의 속성을 (할당)와 __init__()
에서 같은 일을 선언 믿고
필자는 링크 된 질문에 대한 대답을 주석으로 달았습니다. (이전에 했었지만 그 이후로는 잊었습니다.) 설명자 인 인스턴스 속성은 다시 할당 된 경우 할당 된 값의 유형으로 리바운드됩니다 (예 :'self.pressed = touch .pos'는 디스크립터를 버리고'pressed '를'pos'에 리바운드합니다. – mins
을 입력하면 클래스가 속성 설정을 관리하므로 클래스 유형이 잘못된 경우 새 값을 허용하지 않습니다. – Tshirtman