将函数解除绑定到kivy中的按钮

unbinding a function to a button in kivy

考虑以下代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class First(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        x = Button(text='somebutton')
        x.bind(on_press=lambda*_: print('First press'))
        x.bind(on_press=lambda*_: print('Second press'))

        self.add_widget(x)

    def something(self, *somethingishereignored):
        print("I have something")

class FChooser(App):
    def build(self):
        return First()

if __name__ == '__main__':
    FChooser().run()

此代码的行为是,在我按下 'somebutton' 按钮后,它会打印:

Second press
First press

所以,我用谷歌搜索,发现我应该使用 unbind() 函数,我添加了这个:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class First(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        x = Button(text='somebutton')
        x.bind(on_press=lambda*_: print('First press'))
        x.unbind(on_press=lambda*_: print('First press'))
        x.bind(on_press=lambda*_: print('Second press'))

        self.add_widget(x)

    def something(self, *somethingishereignored):
        print("I have something")

class FChooser(App):
    def build(self):
        return First()

if __name__ == '__main__':
    FChooser().run()

但输出没有改变。它仍然是相同的输出。如何解除绑定?这只是一个最小的例子,我打算使用这个功能来动态绑定和取消绑定一个功能到一个按钮,为同一个按钮添加各种功能。

函数不会解绑,因为你没有引用你绑定的函数。当您使用内联 lambda 时,该方法引用未保存,因此您以后不能使用它来解除绑定。
这将起作用:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class First(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        x = Button(text='somebutton')
        x.bind(on_press=self.press1)
        x.unbind(on_press=self.press1)
        x.bind(on_press=self.press2)

        self.add_widget(x)

    def press1(self, *args):
        print("First press")

    def press2(self, *args):
        print("Second press")


class FChooser(App):
    def build(self):
        return First()

if __name__ == '__main__':
    FChooser().run()

或者这样:

    press1 = lambda*_: print('First press')
    press2 = lambda*_: print('Second press')
    x = Button(text='somebutton')
    x.bind(on_press=press1)
    x.unbind(on_press=press1)
    x.bind(on_press=press2)