【问题标题】:TypeError: unsupported operand type(s) for +: 'instance' and 'instance' Python 2.7TypeError:+ 不支持的操作数类型:'instance' 和 'instance' Python 2.7
【发布时间】:2014-03-05 23:04:30
【问题描述】:

我正在尝试使用 Tkinterface 制作一个简单的计算器,可以进行加法、减法、乘法和除法,但是我遇到了一个问题,当我尝试执行我的程序时,我收到了这个错误:

TypeError: +: 'instance' 和 'instance' 的操作数类型不受支持

我个人不知道为什么会发生这种情况,我一直在环顾四周,发现了类似错误的问题,但“instance”和“instance”却没有。 这是我的代码:

#!/usr/bin/python2.7

#Imports necessary modules

from Tkinter import *
#import Tkinter.messagebox
import time as t
import os

#End of importing

#Defines interface

calc = Tk()
calc.title("GUI Calculator")
calc.geometry("500x350+200+200")

#End of interface definition

#Defines methods

def add(val1, val2):
    global ans
    ans = int(val1 + val2)
    return ans

def sub(val1, val2):
    global ans
    ans = int(val1 - val2)
    return ans

def mul(val1, val2):
    global ans
    ans = int(val1 * val2)
    return ans

def div(val1, val2):
    global ans
    ans = float(val1 % val2)
    return ans

#End of method defining

#Heading
but1Text = StringVar()
but1Text.set("Welcome to the GUI Calculator")
label1 = Label(calc, textvariable=but1Text, height=2)
label1.pack()

#Instruction 1
but2Text = StringVar()
but2Text.set("Enter value number one!")
label2 = Label(calc, textvariable=but2Text, height=2)
label2.pack()

#Val 1 input
num1 = IntVar()
val1 = Entry(calc, textvariable=num1)
val1.pack()

#Instruction 2
but3Text = StringVar()
but3Text.set("Enter value number one!")
label3 = Label(calc, textvariable=but2Text, height=2)
label3.pack()

#Val 2 input
num2 = IntVar()
val2 = Entry(calc, textvariable=num2)
val2.pack()

#Operation selector

but4Text = StringVar()
but4Text.set(None)
operator = Radiobutton(calc, text = "Addition", value = "Add", variable = but4Text,command = add(val1,val2)).pack()
operator = Radiobutton(calc, text = "Subtract", value = "Sub", variable = but4Text,command = sub(val1,val2)).pack()
operator = Radiobutton(calc, text = "Multiply", value = "Mul", variable = but4Text,command = mul(val1,val2)).pack()
operator = Radiobutton(calc, text = "Divide",   value = "Div", variable = but4Text,command = div(val1,val2)).pack()

#Answer
but5Text = StringVar()
but5Text.set("Answer=")
label4 = Label(calc, textvariable=but5Text, height=2)
label4.pack()

#Answer
but6Text = StringVar()
but6Text.set(ans)
label5 = Label(calc, textvariable=but6Text, height=2)
label5.pack()

#Mainloop
calc.mainloop()

这是完整的错误:

Traceback (most recent call last):
  File "./tkCalc.py", line 76, in <module>
    operator = Radiobutton(calc, text = "Addition", value = "Add", variable = but4Text,command = add(val1,val2)).pack()
  File "./tkCalc.py", line 24, in add
    ans = int(val1 + val2)
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'

【问题讨论】:

  • 请提供完整的追溯
  • val1val2 是 Entry 对象。你为什么希望val1 + val2 工作?
  • 我希望用户输入 2 个值,并将它们用作计算器的数字
  • 已添加完整的回溯

标签: python tkinter instance calculator


【解决方案1】:

您需要对控件实例中文本的数值(int 或 float)执行操作,而不是控件本身。

试试:

def add(num1, num2):
    global ans
    ans = int(num1.get() + num2.get())
    return ans

operator = Radiobutton(calc, text = "Addition", value = "Add", variable = but4Text,command = add(num1, num2)).pack()

所以你的添加按钮传递变量,函数从中获取值。

【讨论】:

  • 对不起,我是半新的 Python,所以请你解释一下我会改变什么?
  • @Hansmoehansen 扩展了答案。
  • 谢谢,我试试看!
  • 我已经更改了我的代码,并且它启动了,但是我将如何从方法/函数中获取输出,我是否应该这样做,因为我已经拥有它的代码,还是应该更改它? (我已经这样做了,界面上的显示没有更新)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2018-04-27
  • 2010-11-16
  • 2016-03-19
  • 2017-08-24
相关资源
最近更新 更多