获取属性错误:"int"对象没有属性"isnumeric"错误



在这里,我试图使用tkinter构建一个简单的计算器,我使用了一些数字图像作为按钮,我只想在输入框中输入数字和数学字符,但当我按下数字按钮时,我得到了属性错误:'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()方法需要一个字符串,并检查字符串中的字符是否为数字。如果您已经将n作为整数传递到def press(n)中,则没有理由检查它是否是数字,并且它需要一个字符串,这就是您获得AttributeError: 'int' object has no attribute 'isnumeric'的原因。您的输入应该是字符串,而不是int文本。

The python isnumeric() method expects a string and checks if the characters in the string are numeric

正如bpiekars所说,您可以尝试:

str(n).isnumeric()

最新更新