python - 不确定为什么当它是一种方法时它是不可调用的
问题描述
所以我不确定为什么我的一种方法不会覆盖当前的 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
解决方案
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 会改变任何行为
如果您想从“加号按钮”传递对其他按钮标签的引用,您可能需要查看部分功能
推荐阅读
- javascript - 使用 Node JS 从 MySQL 数据库中删除一行
- node.js - Nodejs从函数返回结果是空的承诺
- php - 如何为某些 WordPress 用户角色隐藏 Adsense Java 脚本?
- python - 如何删除 NDB 模型中包含特定属性的实体?
- laravel - Nginx 多位置 laravel 项目
- javascript - 气体计算——找不到错误
- android - 为什么在最小化应用程序并返回后会显示导航栏?
- python - Django 错误:类型对象“Invoice_Line”没有属性“对象”
- javascript - 来自表单输入的Javascript多维数组循环
- android - 是否有 ADB 命令可以知道应用程序主页加载完成时的时间戳?