1

I got a bit of a noob question.

I want to add the floats from the 3 entries, so the total float is shown in the label.

My solution is this and I can't get it to work, what I'm doing wrong?

Thx in advance..

from tkinter import *

class App(Frame):
    def __init__(self, root=None):
        Frame.__init__(self, root)

        self.var = DoubleVar()
        self.var.set(float(0.00))
        self.var2 = DoubleVar()
        self.var2.set(float(0.00))
        self.var3 = DoubleVar()
        self.var3.set(float(0.00))

        self.entry1 = Entry(root, textvariable=self.var)
        self.entry1.grid(row=2, column=1)
        self.entry2 = Entry(root, textvariable=self.var2)
        self.entry2.grid(row=3, column=1)
        self.entry3 = Entry(root, textvariable=self.var3)
        self.entry3.grid(row=4, column=1)


        self.total = DoubleVar()
        self.total.set(float(0.00))

        self.totalSum = Label(root, textvariable=self.total, relief='sunken')
        self.totalSum.grid(row=5, column=1)


    def totalSum(self):
        self.total.set(self.var.get() + self.var2.get() + self.var3.get())



root = Tk()
app = App(root)
root.title("help")
root.mainloop()
Math
  • 141
  • 2
  • 16
  • 1
    You have a method called `.totalSum`, but you shadow that with a Label called `.totalSum`. – PM 2Ring May 23 '18 at 21:44
  • 1
    Your `totalSum` method is never called. If I understand correctly, you want the sum to update automatically every time one of the 3 numbers changes? – Aran-Fey May 23 '18 at 21:44
  • Arh I can see that @Aran-Fey And your right, thats exactly what I want.I tried to change my function name to sumTotal, but dosent work. – Math May 23 '18 at 21:53

1 Answers1

0

The totalSum method is never called.

Ive created a button that calls the method and calculates the sum.

Also, ive changed the method name since you already have a label called totalSum.

And the method calculateTotalSum needs to be called inside the __init__ method.

Here's the working code:

from tkinter import *

class App(Frame):

    def __init__(self, root=None):
        Frame.__init__(self, root)

        self.var = DoubleVar()
        self.var.set(float(0.00))
        self.var2 = DoubleVar()
        self.var2.set(float(0.00))
        self.var3 = DoubleVar()
        self.var3.set(float(0.00))

        self.entry1 = Entry(root, textvariable=self.var)
        self.entry1.grid(row=2, column=1)
        self.entry2 = Entry(root, textvariable=self.var2)
        self.entry2.grid(row=3, column=1)
        self.entry3 = Entry(root, textvariable=self.var3)
        self.entry3.grid(row=4, column=1)


        self.total = DoubleVar()
        self.total.set(float(0.00))

        self.totalSum = Label(root, textvariable=self.total, relief='sunken')
        self.totalSum.grid(row=5, column=1)

        def calculateTotalSum():
            self.total.set(self.var.get() + self.var2.get() + self.var3.get())

        self.button = Button(root, text="Calculate", command=calculateTotalSum)
        self.button.grid(row=6, column=1)


root = Tk()
app = App(root)
root.title("help")
root.mainloop()
Flaming_Dorito
  • 486
  • 3
  • 11
  • Isent it possible to get it to update automatic? Without a button. – Math May 23 '18 at 22:02
  • @Math There is no event that happens when the values of an EntryBox value is changed. If there was it could be possible to call the method, but it isn't possible as it require's an event. – Flaming_Dorito May 23 '18 at 22:06
  • @Math Sort of. You can bind Event handlers to each Entry that call the summing method whenever a key or mousebutton is pressed. Eg, using your original method name, `self.entry1.bind("", self.totalSum); self.entry1.bind(" – PM 2Ring May 23 '18 at 22:12
  • @PM2Ring Yes but that still wouldn't be automatic and requires a key press. – Flaming_Dorito May 23 '18 at 22:15
  • How can you change the value of an Entry without using a key press or a mousebutton press? – PM 2Ring May 23 '18 at 22:23
  • Why do you say "the method `calculateTotalSum` needs to be called inside the `__init__` method"? You _can_ define a callback function inside `__init__` like your version does, but it's also possible to use a method like the OP's `totalSum` as the callback. Of course, it needs to have the appropriate argument signature. – PM 2Ring May 23 '18 at 22:27
  • @PM2Ring I tried but it didn't work. – Flaming_Dorito May 23 '18 at 22:29