首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在kivy "instance“中是什么?像on_pressed(self,instance,pos)

在kivy "instance“中是什么?像on_pressed(self,instance,pos)
EN

Stack Overflow用户
提问于 2016-06-15 23:27:32
回答 2查看 2.1K关注 0票数 4

我想知道instance这个词在kivy中的意思是什么?

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2016-06-21 21:59:46

' instance‘是按下按钮时Class CustomBnt的对象实例的名称和引用。它不一定要命名为'instance‘。你也可以称它为'obj‘或'btn’,或者任何对你有意义的东西。

您可以使用它来收集有关按下的按钮的信息。实例应该是按钮上的文本,例如使用print type(instance)来找出‘instance.text’是什么类型的对象。

票数 4
EN

Stack Overflow用户

发布于 2016-06-16 04:14:21

instance没有特殊的含义。此参数用于向方法传递哪个对象触发了事件。考虑附加到来自另一个类的事件的事件处理程序:

代码语言:javascript
复制
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将知道哪个对象发送了事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37839665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档