Aleksandar Beat
Aleksandar Beat

Reputation: 211

Summing values from entries that are created with button Python

I made a Tkinter program that should create new entry boxes with a button click ("add") and sum values from those entry boxes with a button ("sum") and insert them in entry box for the answer. I keep getting this error:

x +=float(EntryNew.get())
NameError: name 'EntryNew' is not defined

Can you please tell me what am I doing wrong? This is the code:

from tkinter import *
from tkinter import ttk

myApp = Tk()
myApp.title("Kalkulator za materijal")                         
myApp.geometry("450x850")

Label1=Label(myApp, text="Answer")
Label1.grid(row=0,column=0)
Entry1=Entry(myApp)
Entry1.grid(row=1,column=0)


ColumnNumber=1
class ConcreteElement(object):
    def NewElement(self):

        global ColumnNumber
        ColumnNumber +=1

        LabelNew=Label(myApp, text="New Entry")       
        LabelNew.grid(row=0,column=ColumnNumber)

        EntryNew=Entry(myApp)       
        EntryNew.grid(row=1,column=ColumnNumber)

    def summing(self):
        x=0
        x +=float(EntryNew.get())

        Entry1.delete(0,"end")
        Entry1.insert(0,x)


    def __init__(self):

        newbutton=Button(myApp, text="add", command=self.NewElement)
        newbutton.grid(row=4,column=0)

        buttonsum=Button(myApp, text="sum", command=self.summing)
        buttonsum.grid(row=2,column=0)

ConcreteElement=ConcreteElement()      

myApp.mainloop()

Upvotes: 0

Views: 1056

Answers (1)

scotty3785
scotty3785

Reputation: 7006

With classes, you have no need to use globals. At the moment you have a strange mix between global tkinter widgets and those owned by the ConcreteElement class. I've created a new version of your code that might show you how to better achieve your end result. Based on your description, the below should work

from tkinter import *
from tkinter import ttk

myApp = Tk()
myApp.title("Kalkulator za materijal")                         
myApp.geometry("450x850")


class ConcreteElement(object):
    def __init__(self,myApp):
        self.Label1=Label(myApp, text="Answer")
        self.Label1.grid(row=0,column=1)
        self.ColumnNumber = 1

        #Create a blank list to store our entries
        self.EntryBoxList = []

        #Create the answer entry box
        self.Answer=Entry(myApp)
        self.Answer.grid(row=1,column=self.ColumnNumber)


        newbutton=Button(myApp, text="add", command=self.NewElement)
        newbutton.grid(row=4,column=1)

        buttonsum=Button(myApp, text="sum", command=self.summing)
        buttonsum.grid(row=2,column=1)



    def NewElement(self):

        self.ColumnNumber +=1

        self.LabelNew=Label(myApp, text="New Entry")       
        self.LabelNew.grid(row=0,column=self.ColumnNumber)

        #Create a new entry box and add it to the list
        EntryNew=Entry(myApp)       
        EntryNew.grid(row=1,column=self.ColumnNumber)
        self.EntryBoxList.append(EntryNew)

    def summing(self):
        x=0
        for box in self.EntryBoxList:
            x += float(box.get())


        self.Answer.delete(0,"end")
        self.Answer.insert(0,x)


ConcreteElement=ConcreteElement(myApp)      

myApp.mainloop()

You will also note I've added a list to the class. With you orignal code you had no way of remembering what boxes you had created. By adding a list, I can now iterate though all the boxes and sum the contents.

Upvotes: 1

Related Questions