I attempted to build a simple calculator that displays the current sum as a label that changes when numbers are added or subtracted via the "ADD" and "SUBTRACT" buttons through the entry window. The widget is functional only visually but cannot calculate the values I want.

I attempted to add a currentsum value set to 0

from tkinter import*

def addition():
    currentsum=float(e1.get())
    e1.insert(INSERT,str(currentsum))

def subtraction():
    currentsum=currentsum-float(e1.get())
    e1.insert(INSERT,str(currentsum))

def reset():
    currentsum=0
    e1.insert(INSERT,str(currentsum))
window=Tk()

currentsum=0

l1=Label(window,text="current sum:")
l1.grid(row=0, column=0)
l2=Label(window,text=str(currentsum))
l2.grid(row=0,column=1)

e1=Entry(window)
e1.grid(row=1,column=0)


b1=Button(window,text="ADD(+)",command=addition)
b2=Button(window,text="SUBTRACT(-)",command=subtraction)
b3=Button(window,text="RESET",command=reset)
b1.grid(row=2,column=0)
b2.grid(row=2,column=1)
b3.grid(row=2,column=2)

window.mainloop()

Here is the error message:

Exception in Tkinter callback
Traceback (most recent call last):

  File "C:\Users\Administrator\Downloads\sdfgasgasg.py", line 8, in subtraction
    currentsum=currentsum-float(e1.getenter code here())
UnboundLocalError: local variable 'currentsum' referenced before assignment

1 Answers

1
rayryeng On

You are attempting to use currentsum in your methods in a global context but the variable isn't handled properly in a local context in your methods. The reason why is because currentsum is defined after your methods. Move that so it's defined before the methods. Also, if you want to ensure that the variable maintains its changes when each method is called, the "easiest" way to do this is to insert global currentsum in all of your methods so that you're able to access currentsum and modify it

In addition, I would suggest replacing the text that displays the current sum with the running total and not showing this by inserting this into the text field. Inserting will append what's currently in the text field with currentsum which is probably not what you intended. Also, there's a bug in your addition method where you should accumulate the value read in from the text field, not replacing it.

Therefore:

from tkinter import*

currentsum=0

def addition():
   global currentsum # New
   currentsum+=float(e1.get()) # Fix
   #e1.insert(INSERT,str(currentsum))
   l2['text'] = str(currentsum) # Change

def subtraction():
   global currentsum # New
   currentsum=currentsum-float(e1.get())
   #e1.insert(INSERT,str(currentsum))
   l2['text'] = str(currentsum) # Change

def reset():
   global currentsum # New
   currentsum=0
   #e1.insert(INSERT,str(currentsum))
   l2['text'] = str(currentsum) # Change
window=Tk()

l1=Label(window,text="current sum:")
l1.grid(row=0, column=0)
l2=Label(window,text=str(currentsum))
l2.grid(row=0,column=1)

e1=Entry(window)
e1.grid(row=1,column=0)


b1=Button(window,text="ADD(+)",command=addition)
b2=Button(window,text="SUBTRACT(-)",command=subtraction)
b3=Button(window,text="RESET",command=reset)
b1.grid(row=2,column=0)
b2.grid(row=2,column=1)
b3.grid(row=2,column=2)

window.mainloop()