I create a function to bind it with a but so when i press the button all all the entries in my programm will be cleared. When i run the function, only the last one is being cleared.

P.S. all the entries are generated with for, in the same way i try to clear them.

def refresh():
    for i in range (1,a+1):
        entrylength_i.delete(0, "end")
        entrywidth_i.delete(0, "end")
        entryheight_i.delete(0, "end")
    for j in range (1,b+1):
        entrylength_j.delete(0, "end")
        entrywidth_j.delete(0, "end")
        entryheight_j.delete(0, "end")

1 Answers

1
Henry Yik On Best Solutions

Since you mentioned you created your entries the same way you try to clear them, I assume you do something like:

a= 5
e = []

for i in range (1,a+1):
    entrylength_i = tk.Entry(root)
    entrylength_i.grid(row=i,column=0)
    e.append(entrylength_i)

print (e)

Result:

[<tkinter.Entry object .!entry>, <tkinter.Entry object .!entry2>, ...]

In the for loop, you are reusing the variable name entrylength_i over and over (instead of entrylength_1, entrylength_2...). It works because you are creating a new object in each iteration.

But when you try to remove them using the same technique:

for i in range (1,a+1):
    entrylength_i.delete(0, "end")

The variable entrylength_i only points to the object it last defined, which is why only last one is being cleared.

Therefore the proper way to clear all the entries is to iterate through the objects in your list e:

for entry in e:
    entry.delete(0, "end")