我正在使用Python 3.我不知道我的代码在哪里出错了。它一直弹出错误信息
from tkinter import *
class calculator():
def __init__(self):
window=Tk()
window.title("Calculator")
self.var1=StringVar()
self.var2=StringVar()
number1=Label(window,text="Number_1 =").grid(row=1,column=1)
number2=Label(window,text="Number_2 =").grid(row=2,column=1)
num1=Entry(window,textvariable=self.var1).grid(row=1,column=2)
num2=Entry(window,textvariable=self.var2).grid(row=2,column=2)
plus=Button(window,text="+",command=self.addition).grid(row=3,column=1)
minus=Button(window,text="-",command=self.subtraction).grid(row=3,column=2)
times=Button(window,text="*",command=self.multiplication).grid(row=3,column=3)
divides=Button(window,text="/",command=self.division).grid(row=3,column=4)
self.result=Label(window,text="result",bg="black",fg="white").grid(row=3,column=5)
window.mainloop()
def addition(self):
self.result["text"]= str(float((self.var1.get())) +(float(self.var2.get())))
def subtraction(self):
self.result["text"] = str(float((self.var1.get())) +(float(self.var2.get())))
def multiplication(self):
self.result["text"] = str(float((self.var1.get())) +(float(self.var2.get())))
def division(self):
self.result["text"] = str(float((self.var1.get())) +(float(self.var2.get())))
calculator()
错误陈述说,
self.result["text"]= str(float((self.var1.get())) +(float(self.var2.get())))
TypeError: 'NoneType' object does not support item assignment
答案 0 :(得分:1)
很常见的问题
如果你这样做
self.result = Label(...).grid()
然后你为None
提供帮助,因为grid()
/ pack()
/ place()
会返回None
所以你有self.result = None
,后来你遇到了问题
你需要分两行
self.result = Label(...)
self.result.grid()