python - 获取 AttributeError:“int”对象没有属性“isnumeric”错误
问题描述
在这里,我正在尝试使用 tkinter 构建一个简单的计算器,并且我使用了一些数字图像作为按钮,我只想在输入框中输入数字和数学字符,但是当我按下数字按钮时,我得到AttributeError: 'int'对象没有属性“isnumeric”错误,我没有得到解决这个问题的方法:这是我的代码,下面的代码是 tkinter 按钮的功能:
def press(n):
new=value.get()
if new=="Can't divide by zero" or new=="Can't perform operation":
new=''
if n.isnumeric() or n=='+' or n=='-' or n=='*' or n=='/' or n=='%' or n=='.':
new+=str(n)
value.set(new)
解决方案
pythonisnumeric()
方法需要一个字符串并检查字符串中的字符是否为数字。如果您已经将 ndef press(n)
作为整数传递,则没有理由检查它是否为数字并且它需要一个字符串,这就是您获得AttributeError: 'int' object has no attribute 'isnumeric'
. 您的输入应该是字符串,而不是 int 文字。
推荐阅读
- java - JPA 多对一关系和外部表的问题
- python - Pytorch:尽管矩阵的大小匹配(m1:[256 x 200],m2:[256 x 200]),但大小不匹配错误
- swift - 在 Swift 中将地图从 Firestore 转换为字典
- javascript - jQuery:使用 jQuery 保存表单会使表单无效
- android - 我不能在 androidx.appcompat:appcompat:1.1.0 上使用 API 反射
- c - 如何在函数内初始化结构数组?
- javascript - 如何在不查询所有集合的情况下检查我的用户中是否存在字段
- reactjs - GitHub Primer React 组件与 Octions 的使用
- python-3.x - 使用 Nd 数组切片 N+1d Numpy 数组
- javascript - 在 VueJS 中输出动态值?