gpt4 book ai didi

button - 将函数解除绑定(bind)到kivy中的按钮

转载 作者:行者123 更新时间:2023-12-05 02:16:08 25 4
gpt4 key购买 nike

考虑以下代码:

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()

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

最佳答案

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

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)

关于button - 将函数解除绑定(bind)到kivy中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50639326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com