-1

I'm creating a simple calculator that add two integer and show the result on the line A+B. But when I click the button and the error show up, can you guys please explain for me what's wrong and how to fix it, thank you guys so much.

from tkinter import *
parent = Tk()
A= Label(parent, text = "A").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
B= Label(parent, text = "B").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
AaddB=Label(parent, text = "A+B").grid(row = 6, column = 0)
def AaddB():
    print (A+B)
    
submit = Button(parent, text = "PLUS",command = AaddB).grid(row = 4, column =1)
   
parent.mainloop()

enter image description here

MinhTris
  • 15
  • 1
  • 6
  • Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – Thingamabobs Nov 19 '21 at 13:41
  • Thank you so much, I just try that solution, and it didn't fix my error, and my error is: TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' – MinhTris Nov 19 '21 at 14:07
  • If you have fixed that, you need additionally fix this line to `print (int(A.get())+int(B.get()))` – Thingamabobs Nov 19 '21 at 14:16
  • If you see your code carefully, you will notice that ```A``` and ```B``` are just ```Label```s. You can't add or calculate a widget. So assign some other name for those labels and below ```AaddB()``` function, add this: ```A = int(e1.get())``` and on the next line, ```B = int(e2.get())``` Also, you should avoid creating and gridding the widget on the same line/variable. It can cause errors. – IJ_123 Nov 19 '21 at 14:27
  • You have used ```e1 = Entry(...).grid(...)```, which returns ```None```. Why not create and grid it separately? Example: ```e1 = Entry(...)```, on the next line: ```e1.grid(...)``` – IJ_123 Nov 19 '21 at 14:30

1 Answers1

0
from tkinter import *
tinhtong = Tk()
tinhtong.title("Tính tổng A+B") 
tinhtong.geometry("300x300")
A= Label(tinhtong, text = "A=").grid(row = 0, column = 0)
A1 = Entry(tinhtong)
A1.grid(row = 0, column = 1)
B= Label(tinhtong, text = "B=").grid(row = 1, column = 0)
B2 = Entry(tinhtong)
B2.grid(row = 1, column = 1)
def AcongB():
    A = int(A1.get())
    B = int(B2.get())
    Tong.set(A+B)
    Tong=StringVar()
submit = Button(tinhtong, text = "Tổng",command=AcongB)
submit.grid(row = 4, column = 1)
AcongB=Label(tinhtong, text = "A+B=").grid(row=6,column=0)
AcongB=Label(textvariable=Tong)
AcongB.grid(row=6,column=1)
tinhtong.mainloop()
MinhTris
  • 15
  • 1
  • 6