leonardik
leonardik

Reputation: 131

TKinter - how to add two figures from Entry()

I need to create a calculator which adds to figures entered with Entry() My current code returns no result - I see only fields and the button Please help

from tkinter import *
a=Entry()
b=Entry()
def take():
    aa=float(a.get())
    bb=float(b.get())
    cc=aa+bb
root = Tk()
    Button(text="Calc", command=take).pack()
a.pack()
b.pack()
cc = Label(width=10, height=10)
cc.pack()
root.mainloop()

Upvotes: 1

Views: 29

Answers (1)

FrainBr33z3
FrainBr33z3

Reputation: 1105

cc is a Label object in the program, and the variable name is also locally used in the function take.

By assigning cc=aa+bb in the function, generates a local variable, but doesn't print this value on the root window.

To print the value, you need to configure the Label widget cc as:

cc.config(text=aa+bb) # Replace cc=aa+bb

Upvotes: 1

Related Questions