简体   繁体   English

Kivy:绑定两个方法,一个接一个地调用

[英]Kivy: bind two methods to be called one after the other

I have a class C with two methods, a and b, and i want one of them (b) to be always called after the other (a) 我有一个C类,有两个方法,a和b,我希望其中一个(b)总是在另一个之后调用(a)

class C(Widget) :
 def __init__(self) :
  super(C, self).__init__() 
  self.bind(a=self.b)  # something like this 
 def a(self) : pass
 def b(self) : pass

How? 怎么样?

How about calling b from a. 如何从a调用b。

def a(self) :
    #some code
    self.b()

I don't quite remember why doesn't it work through binding, but I think it has something to do with using different objects eg binding to an old method that isn't executed or something in that sense. 我不太清楚为什么它不能通过绑定工作,但我认为它与使用不同的对象有关,例如绑定到一个未执行的旧方法或那种意义上的东西。

This is where super() comes to the rescue. 这就是super()来救援的地方。 You see, super() isn't only for calling __init__() of the class you inherit from, it has other purposes too. 你看, super()不仅用于调用你继承的类的__init__() ,它还有其他用途。

I just replaced __init__() with <overridden method>() : 我刚用<overridden method>()替换__init__() <overridden method>()

from kivy.base import runTouchApp
from kivy.uix.button import Button

class MyBase(Button):
    def overridden(self, *args):
        self.stable()

    def stable(self, *args):
        print('stable')

class MyButton(MyBase):
    def __init__(self, **kwargs):
        super(MyButton, self).__init__(**kwargs)
        self.bind(on_release=self.overridden)

    def overridden(self, *args):
        # run before overriden stuff
        super(MyButton, self).overridden()

        print('new overridden')

        # run after overridden stuff
        super(MyButton, self).overridden()
runTouchApp(MyButton())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM