hi , below is my program which is supposed to accept 3 different numbers , add them up and print the result but what it does is that it takes the first number and mutiplies that by 3 , where have I gone wrong ?

from Tkinter import *

def update(event):
    amount = 0
    for x in range(3):
        
        amnt=float(e.get())
        amount=amount + amnt
        lab.config(text='total='+ str(amount))
        
#        e.delete(0,END) ,I thought this would solve the problem but it did not
root = Tk()
label1=Label(root,text="please enter amount")
label1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1)
e.grid(row=0,column=1)
e.focus_set()
e.bind('<Return>',update)
lab=Label(root,text='')
lab.grid(row=5,columnspan=2)
root.mainloop()

You enter a number (say 10), press Enter and the update() function does the following:
- it sets amount to 0
- reads the number from the Entry (10), adds it to amount (10), sets the label text to amount (10)
- reads the number from the Entry (10), adds it to amount (20), sets the label text to amount (20)
- reads the number from the Entry (10), adds it to amount (30), sets the label text to amount (30)
The problem is, it does this faster than you can clear the Entry and input a new number, so it uses always the first number :) That's where you've gone wrong.

hi mawe , that is what I thought but I could not figure out how to solve it , any sugestions ?

Well, it's true that e.delete(0,END) didn't solve your problem ... but it did give you a very useful error message:

>>> 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:/Python24/src/adder.py", line 7, in update
    amnt=float(e.get())
ValueError: empty string for float()

It seems clear that the update routine is reading, then clearing, then reading again ... without any intervening user input.

Try eliminating the loop and see what happens.

Jeff

hi jeff , I have tried everything , I even eliminated the loop and all it did was to just read in the numbers but not add them
I just don't know what else to do , I even tried the loop inside the main program and still did not work , need more help
ali

Try this:

from Tkinter import *

def update(event):
    
    amnt=float(e.get())
    lab.total += amnt
    lab.config(text='total='+ str(lab.total))
    e.delete(0,END) #,I thought this would solve the problem but it did not
root = Tk()
label1=Label(root,text="please enter amount")
label1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1)
e.grid(row=0,column=1)
e.focus_set()
e.bind('<Return>',update)
lab=Label(root,text='')
lab.total = 0
lab.grid(row=5,columnspan=2)
root.mainloop()

It doesn't stop after three numbers, but that should be easy to add.

Jeff

hi jeff , thanks for the code . this afternoon I spent some time examining my code line by line and it turned out that the line which adds the amount never works, in your code you are using lab.total , this is the first time I have seen this , can you explain this to me
thanks again ali

Ah, yes, I wondered if that was the problem. Start here:

def update(event):
    total = 0
    amnt=float(e.get())
    total += amnt
    lab.config(text='total='+ str(lab.total))
    e.delete(0,END)

This code would add to total, but after the method is done, all local variables, including total, are forgotten. They 'go out of scope.'

Thus, total restarts at 0 every time the method runs.

What is needed is some permanent way to store the total. One possibility is to make total a global variable. The other, better way is to make total a part of the label itself, which is what I do.

lab.total = 0

creates a new property of lab called 'total.' Because lab is a permanent, global object, the total is also permanent.

IMO, I should have made total a part of the entry widget instead of the label, but either way works fine.

I think one thing you may have overlooked is that the update method gets called once per <Enter>. That's why the loop had no chance of working.

Jeff

thanks jeff , I appreciate spending time answering my question . thanks again and take care ali

This article has been dead for over six months. Start a new discussion instead.