python - Kivy - 解除绑定(bind)到按钮的所有方法

标签 python events bind kivy unbind

我正在编写一个程序,在右侧有一个按钮面板,它成功地将一个方法绑定(bind)到每个按钮,具体取决于用户输入/操作。我的问题是我不能 unbind()方法明确,因为方法绑定(bind)是动态的。

考虑;

    i = 1
    while i <= 8:
        string = "action" + str(i) 
        #Buttons named 'action1, action1, ..., action8'
        ref = self.ids[string] 
        ref.text = ""
        observers = ref.get_property_observers('on_release')
        print observers
        ref.unbind()
        ref.bind(on_release=partial(self.BlankMethod, arg1))
        i += 1

线条;

        observers = ref.get_property_observers('on_release')
        print observers

每次调用该方法时,表明我绑定(bind)了越来越多的弱方法列表,但解除绑定(bind)函数不会解除绑定(bind)该方法。

虽然我的代码示例没有显示,但绑定(bind)方法会定期更改,并且 self.BlankMethod旨在覆盖原始绑定(bind)。事实并非如此,Button的绑定(bind)方法增加。

[<kivy.weakmethod.WeakMethod object at 0x7f8cc826a810>] [<kivy.weakmethod.WeakMethod object at 0x7f8cc826a850>, <kivy.weakmethod.WeakMethod object at 0x7f8cc826acd0>]

我试过了;

        observers = ref.get_property_observers('on_release')
        if observers != []:
            for observer in observers:
                ref.unbind(observer)
        ref.bind(on_release=partial(self.Blank))

但是我返回了错误;

TypeError: unbind() takes exactly 0 positional arguments (1 given)

我看过使用 funbind()函数但随后给出;

AttributeError: 'Button' object has no attribute 'funbind'

尝试使用 fbind()funbind() 之前也给出相同的错误,但关于没有 fbind() 的按钮属性。

如何列出对象的所有绑定(bind)方法,然后解除绑定(bind)?

最佳答案

这是一个玩具示例,演示设置、保存和稍后清除回调。当按下 a_buttonb_button 时,将触发 set_callbacks,它将回调绑定(bind)到所有 MyButtonMyButton 有一个列表属性 _cb 存储用户定义的回调。 c_button 将触发 clear_callbacks,它遍历每个 MyButton_cb 列表并解除绑定(bind)每个存储的回调。

from kivy.app import App
from kivy.lang import Builder
from functools import partial

kv_str = """
<MyButton@Button>:
    _cb: []
    my_id: 1
    text: "hello {}".format(self.my_id)
BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        Button:
            id: a_button
            text: "a button"
        Button:
            id: b_button
            text: "b button"
        Button:
            id: c_button
            text: "clear callbacks"
    GridLayout:
        cols: 4
        id: grid
        MyButton:
            my_id: 1
        MyButton:
            my_id: 2
        MyButton:
            my_id: 3
        MyButton:
            my_id: 4
"""

def cb(sender, arg='nothing'):
    print "we got: {} pressed with {}".format(sender.text, arg)

class MyApp(App):
    def build(self):
        root = Builder.load_string(kv_str)

        def set_callbacks(sender):
            for b in root.ids.grid.children:
                new_cb = partial(cb, arg=sender.text)
                b._cb.append(new_cb)
                b.fbind('on_press', new_cb)
        def clear_callbacks(sender):
            for b in root.ids.grid.children:
                for cb in b._cb:
                    b.funbind('on_press', cb)
                b._cb = []
        root.ids.a_button.bind(on_press=set_callbacks)
        root.ids.b_button.bind(on_press=set_callbacks)
        root.ids.c_button.bind(on_press=clear_callbacks)
        return root

if __name__ == '__main__':
    a = MyApp()
    a.run()

关于python - Kivy - 解除绑定(bind)到按钮的所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35328271/

相关文章:

Python 'If not' 语法

python - 从 linux 命令行一次启动 python 脚本的多个实例

java - Android - 如何处理两指触摸

r - 将数据帧列表 : not a simple rbind, 第二行转换为新列

javascript - 使用绑定(bind)确保方法引用对象,但它似乎不起作用

python - OSX 下 pip 中区分大小写的包名称

python - 让django ORM准备sql,而不执行

javascript - 为什么一个按钮会导致我的整个网页重新加载?

c# - 避免在 C# 中重复订阅事件

c - 多播绑定(bind) - 地址已被使用