-5

I'm trying to make a GPA calculator and I want the result as a float. However, I'm getting the following error: cannot convert string to float:...

I searched for similar solutions on this site but found one that didn't work either. It would be helpful if you could give me some hints.

from tkinter import *

class gpa:

    def __init__(self, master):

        master.title("GPA CALCULATOR")

        self.result = StringVar()
        self.res = Entry(master, width = 60, textvariable = self.result)
        self.res.grid(row = 0, column = 0, columnspan = 6,padx = 3, pady = 6,  ipady = 6, sticky = "N E W S" )

        labels = '1st Semester GPA: ', '2nd Semester GPA: ','3rd Semester GPA: ','4th Semester GPA: ','5th Semester GPA: ','6th Semester GPA: ','7th Semester GPA: ','8th Semester GPA: ',


        row = 1
        col = 0
        i = 1
        for label in labels:

            self.l = Label(master, text = label)
            self.l.grid(row = row, column = col, sticky = E)
            row = row  + 1






        self.var1 = StringVar()
        self.var2 = StringVar()
        self.var3 = StringVar()
        self.var4 = StringVar()
        self.var5 = StringVar()
        self.var6 = StringVar()
        self.var7 = StringVar()
        self.var8 = StringVar()


        rows  = 1
        cols = 1

        for x in range(1, 9):


            self.ent = Entry(master, width = 30, textvariable = 'self.var'+str(x) )
            self.ent.grid(row = rows,pady = 6, column = cols, columnspan = 4, sticky = W)
            rows = rows + 1

            print('self.var' + str(x))



        self.button = Button(master, width = 20, text = "Calculate", command = self.calc())
        self.button.grid(row= 10, column = 1,columnspan = 5,   sticky = E)


    def calc(self):

            self.val1 = float(self.var1.get())
            self.val2 = float(self.var2.get())
            self.val3 = float(self.var3.get())
            self.val4 = float(self.var4.get())
            self.val5 = float(self.var5.get())
            self.val6 = float(self.var6.get())
            self.val7 = float(self.var7.get())
            self.val8 = float(self.var8.get())

            self.add = self.val1 + self.val2 + self.val3 + self.val4 + self.val5 + self.val6 + self.val7 + self.val8/8
            print(self.add)

            self.result.set(self.add)



root = Tk()
root.configure(padx =6, pady = 12)

run  = gpa(root)

root.mainloop()
Tagc
  • 8,736
  • 7
  • 61
  • 114
Ahmad
  • 23
  • 1
  • 6

1 Answers1

2

You're setting your StringVar's wrong...

textvariable = "self.var" + str(x)

This is just a string. Not a reference to the StringVar.

In your calc function you're doing this:

 >>> float('')
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 ValueError: could not convert string to float:

Because your StringVar has no "value" -- since the reference to the stringvar wasn't passed correctly it's not being updated, it's by default set to the empty string.

If you wanted to iteratively get StringVar's this way... There's a few options:

Use __dict__: self.__dict__['var%d' % i]

Use getattr: getattr(self, 'var%d' % i)

Put them in a list / tuple and index either. Use a dict index that, although imo that's redundant since using self.* will make it be in the classes namespace... and you can just use the above.

Even correctly using your StringVar's so that they update doesn't guarantee float conversion... They could still be empty if the Entry is empty...

You're also setting your command wrong. command=self.calc(). This is calling the function and setting what it returns to be the command. Use command=self.calc

Pythonista
  • 11,377
  • 2
  • 31
  • 50