Kivy - 解除绑定到按钮的所有方法
Kivy - unbind all methods attached to a button
我正在编写一个程序,在右侧有一个按钮面板,它成功地将一个方法绑定到每个按钮,具体取决于用户 input/actions。我的问题是我不能 unbind()
显式方法,因为绑定的方法是动态的。
考虑一下;
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
显示我绑定的weakmethods列表越来越多,每次方法被调用,但是unbind函数并没有解除方法的绑定。
虽然我的代码示例没有显示,但绑定方法会定期更改,self.BlankMethod
旨在覆盖原始绑定。事实并非如此,Button
的绑定方法增加了。
[<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'
尝试在 funbind()
之前使用 fbind()
也会产生相同的错误,但相对于没有 fbind()
属性的按钮。
如何列出对象的所有绑定方法,然后解除绑定?
这是一个演示设置、保存和稍后清除回调的玩具示例。当按下 a_button
或 b_button
时,会触发 set_callbacks
,这会将回调绑定到所有 MyButton
。 MyButton
有一个列表 属性 _cb
存储用户定义的回调。 c_button
将触发 clear_callbacks
,它遍历每个 MyButton
的 _cb
列表并解除绑定每个存储的回调。
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()
我正在编写一个程序,在右侧有一个按钮面板,它成功地将一个方法绑定到每个按钮,具体取决于用户 input/actions。我的问题是我不能 unbind()
显式方法,因为绑定的方法是动态的。
考虑一下;
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
显示我绑定的weakmethods列表越来越多,每次方法被调用,但是unbind函数并没有解除方法的绑定。
虽然我的代码示例没有显示,但绑定方法会定期更改,self.BlankMethod
旨在覆盖原始绑定。事实并非如此,Button
的绑定方法增加了。
[<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'
尝试在 funbind()
之前使用 fbind()
也会产生相同的错误,但相对于没有 fbind()
属性的按钮。
如何列出对象的所有绑定方法,然后解除绑定?
这是一个演示设置、保存和稍后清除回调的玩具示例。当按下 a_button
或 b_button
时,会触发 set_callbacks
,这会将回调绑定到所有 MyButton
。 MyButton
有一个列表 属性 _cb
存储用户定义的回调。 c_button
将触发 clear_callbacks
,它遍历每个 MyButton
的 _cb
列表并解除绑定每个存储的回调。
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()