hi , I am trying to first ask the user how many numbers he has ( I have not coded this part yet but assumed number 5 ), then enter the first number , hit enter , enter another number in the same entery widget up until the numbers are finished then print the result . the error I am getting is it can not convert string to float , any idea how to solve this ? thanks

from tkinter import *
def update(event):
    sum1=0.0
    i=0
    while i<=5:
        e.delete(0,END)
        value=float(e.get())
        sum1=sum1 + value
        i=i+1
    lbl2.config(text=sum1)

root = Tk()
lbl=Label(root,text='enter the number')
lbl.grid(row=0 , column=0)
value=DoubleVar()
sum1=DoubleVar()
e=Entry(root )
e.grid(row=0,column=1)
e.bind('<Return>' , update)
lbl2=Label(root,text=' ')
lbl2.grid(row=1,column=0)
root.mainloop()

Since Entry() gives a string, you can ask the user to separate the numbers with a semicolon. Then you can split the string and process it accordingly.

For example ...

eget = "123;321;456;2.45;12.8"
numbers = [float(x) for x in eget.split(';')]
print(numbers)
print(sum(numbers))

thanks @vegaseat , did not think of it this way . but how about if numbers are 6 or 7 digits long , it will fill up the space pretty fast and my second question is that why value=float(e.get()) does not make it float ?

The Entry() field keeps scrolling as you fill it up.

If you use value=float(e.get()), the string would have separators like semicolons in it. Function float() can only convert a numeric string. Numbers do not have ; in it.

If you want the user to be able to scroll through what has been entered and potentially correct errors, you can attach a horizontal scrollbar to Entry() ...

''' tk_Entry_scroll_h.py
adding a horizontal scrollbar to Tkinter's Entry()
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk


class TestEntry():
    def __init__(self):
            root = tk.Tk()
            self.scrollbar = tk.Scrollbar(root,orient="horizontal")
            txt = tk.StringVar()
            self.e3 = tk.Entry(root,
                      xscrollcommand=self.scrollbar.set,
                      textvariable=txt)
            txt.set("do add some longer text to scroll") 
            self.e3.focus()
            self.e3.pack(side="bottom",fill="x")
            self.scrollbar.pack(fill="x")
            self.scrollbar.config(command=self.e3.xview)
            self.e3.config()

            root.mainloop()

TestEntry() 

thanks @vegaseat

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.