首页 > 解决方案 > 不确定为什么当它是一种方法时它是不可调用的

问题描述

所以我不确定为什么我的一种方法不会覆盖当前的 on_press 绑定。我尝试使用 .unbind 方法,但也没有用。我尝试使用 lambda 并且只有在我只更改一个按钮时才真正起作用。我想创建一个图表,单击元组后,我可以使用“+”按钮更改数字。当我单击另一个元组时,我可以使用相同的“+”按钮更改该元组。使用 lambda 同时改变了它们

def PlusOne(buttonValue):
    Result = float(buttonValue.text) + 1
    buttonValue.text = str(Result)
    print(str(buttonValue.text))

def ButtonPlusOne(button):
    global x

    if x == 1:
       self.plusButton.unbind(on_press=PlusOne(button))
       self.plusButton.bind(on_press=PlusOne(button))
       print(str(button.text))
    else:
       self.plusButton.bind(on_press=PlusOne(button))
       print(str("ElsePrint " + button.text))
       x = 1

self.Button1.bind(on_press=ButtonPlusOne(self.Button1))
self.Button2.bind(on_press=ButtonPlusOne(self, self.Button2))

这将返回 AssertionError: None is not callable

标签: pythonkivy

解决方案


on_press论点看起来像这样,

self.Button1.bind(on_press=ButtonPlusOne)

这将调用已经ButtonPlusOne()传递的实例self.Button1

该错误是因为 Kivy 正在尝试运行ButtonPlusOne(self.Button1)(self.Button1)or None(self.Button1),这是您得到的错误。


您的PlusOne功能似乎很好(但是,您可能正在做float('+') + 1,这是一个单独的问题),但同样,绑定需要on_press=PlusOne

我不认为将相同的功能重新绑定到 plusButton 会改变任何行为

如果您想从“加号按钮”传递对其他按钮标签的引用,您可能需要查看部分功能


推荐阅读