首页 > 解决方案 > 获取 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)

标签: pythontkinterattributeerror

解决方案


pythonisnumeric()方法需要一个字符串并检查字符串中的字符是否为数字。如果您已经将 ndef press(n)作为整数传递,则没有理由检查它是否为数字并且它需要一个字符串,这就是您获得AttributeError: 'int' object has no attribute 'isnumeric'. 您的输入应该是字符串,而不是 int 文字。


推荐阅读