【问题标题】:Not Sure why this is not Callable when Its a method不确定为什么当它是一种方法时它是不可调用的
【发布时间】:2020-12-02 17:53:55
【问题描述】:

所以我不确定为什么我的一种方法不会覆盖当前的 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

【问题讨论】:

  • 尝试调试它并查看'plusButton'在那个时间点的状态。它是您期望的对象吗?此代码中从未分配 self.plusButton。
  • on_press 是指分配给一个函数,而不是调用一个函数的结果。

标签: python kivy


【解决方案1】:

on_press 参数看起来像这样,

self.Button1.bind(on_press=ButtonPlusOne)

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

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


您的 PlusOne 函数看起来不错(但是,您可能正在使用 float('+') + 1,这是一个单独的问题),但同样,绑定需要是 on_press=PlusOne

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

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

【讨论】:

  • 这成功了!作为一个新的 python/kivy 程序员,这对我来说解释了很多。非常感谢。
猜你喜欢
  • 2012-03-25
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多