-6

I am using Python 3. I don't know where i get this code wrong. it kept popping up with an error message

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()        

The error statement says,

self.result["text"]= str(float((self.var1.get())) +(float(self.var2.get())))

TypeError: 'NoneType' object does not support item assignment
furas
  • 134,197
  • 12
  • 106
  • 148
mm123
  • 1
  • 1
  • Please see [how to ask](https://stackoverflow.com/help/how-to-ask) and edit your post. I don't see a question and the body of your post is entirely code. You mention an error message. What is it? Include the full Traceback. – Galen Dec 22 '17 at 06:00
  • use button `{}` to format code on SO. – furas Dec 22 '17 at 06:08
  • Title should be short. Put question in body, not only in title. – furas Dec 22 '17 at 06:09
  • `'NoneType' ` means that you get `None` in some variable - so you have to check all variables to find out which one gives `None`. – furas Dec 22 '17 at 06:11

1 Answers1

1

Very common problem

If you do

self.result = Label(...).grid()

then you assing None because grid() / pack() / place() returns None

So you have self.result = None and later you have your problem

You have to do in two lines

self.result = Label(...)
self.result.grid()
furas
  • 134,197
  • 12
  • 106
  • 148