0

I have a code that creates 10 entries using a for loop, which saves the result in a list so I can use get, but I have another button and I need to delete what is written in the 10 entries, how do I do that?

    for i in range(10):
        entry = Entry(self.lf_mid)
        entry.place(relwidth=0.6, relheight=1/10, relx=0.35, rely=i/10)
        lista_entrys.append(entry)
    
    #this don't work
    bt = Button(self.lf_bot, text='clear', command=entry.delete(0, 'end'))
    bt.pack(side='left', expand=1)

I appreciate if you can help me <3

Emerson
  • 5
  • 1
  • If all need is to edit some values within all the "entrys", try to just pass through all of them. – eran halperin Dec 05 '21 at 18:30
  • Does this answer your question? [How to clear the Entry widget after a button is pressed in Tkinter?](https://stackoverflow.com/questions/2260235/how-to-clear-the-entry-widget-after-a-button-is-pressed-in-tkinter) – chickity china chinese chicken Dec 05 '21 at 18:33
  • You do it exactly the same way you get the data, by iterating over the list. Have you tried that? – Bryan Oakley Dec 05 '21 at 18:53
  • @chickitychinachinesechicken, no, unfortunately no, it only has an Entry variable, I have a loop with 10, if I try to do that I'll just delete the last entry – Emerson Dec 05 '21 at 21:01
  • okay , @BryanOakley , you were right, forgetting that for a moment the list only saves the id of the entries and not their contents. actually using .delete(0, END) iterating through the list worked, thank you – Emerson Dec 05 '21 at 21:47

1 Answers1

0

make a widget dictionary that you add to every time you create a widget. Then wherever you want to access the widget, for example if you want to destroy it or change its configuration attributes, you do this through the dictionary.

Skeleton of the idea:

import tkinter as tk
widgetdict = {}
def maketk():
    root = tk.Tk()
    entry = tk.Entry()
    widgetdict['entry'] = entry
    root.mainloop()

maketk()
widgetdict['entry']['bg'] = '#fff'
widgetdict['entry'].destroy()

What goes into the dictionary is the pointer/address of the entry. So long as you haven't already destroyed the widget somewhere else then you have access.

InhirCode
  • 338
  • 1
  • 4
  • 5
  • thanks, but that doesn't solve my problem yet, I need to find a way to clear the input field so I can dynamically fill it with different values ​​and save again – Emerson Dec 05 '21 at 21:14
  • ok so indent your button section to go inside the for loop. This would give a button for each entry with the option to delete that entry. If you need to clear all entries in one go outside the for loop, then iterate through the widget dictionary, '''for w in widgetdict: widgetdict[w].delete(0,'end') – InhirCode Dec 05 '21 at 21:18
  • I already managed to do what I needed, the information I wanted was saved in a list that I forgot that saved the entry id and not the value, but thank you friend. – Emerson Dec 07 '21 at 00:14