我想知道instance这个词在kivy中的意思是什么?
class CustomBtn(Widget):
pressed = ListProperty([0, 0])
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.pressed = touch.pos
# we consumed the touch. return False here to propagate
# the touch further to the children.
return True
return super(CustomBtn, self).on_touch_down(touch)
def on_pressed(self, instance, pos):
print ('pressed at {pos}'.format(pos=pos))
print ('My callback is call from', instance)发布于 2016-06-21 21:59:46
' instance‘是按下按钮时Class CustomBnt的对象实例的名称和引用。它不一定要命名为'instance‘。你也可以称它为'obj‘或'btn’,或者任何对你有意义的东西。
您可以使用它来收集有关按下的按钮的信息。实例应该是按钮上的文本,例如使用print type(instance)来找出‘instance.text’是什么类型的对象。
发布于 2016-06-16 04:14:21
instance没有特殊的含义。此参数用于向方法传递哪个对象触发了事件。考虑附加到来自另一个类的事件的事件处理程序:
class MyLabel(Label):
def pressed_handler(self, instance, pos):
print ('pressed at {pos}'.format(pos=pos))
print ('My callback is call from', instance)
a = CustomBtn()
b = MyLabel()
a.bind(on_pressed=b.pressed_handler)然后,通过instance参数,pressed_handler将知道哪个对象发送了事件。
https://stackoverflow.com/questions/37839665
复制相似问题