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