Search code examples
pythontkintertkinter-entry

Add two integer on Tkinter


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


Solution

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