Python Forum
[Tkinter] Clearing listboxes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Clearing listboxes (/thread-36243.html)



Clearing listboxes - klatlap - Feb-01-2022

I currently use this function to clear multiple listboxes, but i feel is not a good way to do it and am sure can be done with a for loop but can't work it out.
def delete_result():  # used in refreshing the data
    runner1.delete(0, tk.END)
    fw1.delete(0, tk.END)
    fp1.delete(0, tk.END)
    vw1.delete(0, tk.END)
I will be adding many more listboxes so need a better option.


RE: Clearing listboxes - menator01 - Feb-01-2022

Not tested but, might could put them in a list. Something like
my_listboxes = ['runner1', 'fw1', 'fp1', 'vw1']
for listbox in my_listboxes:
    listbox.delete(0, tk.END)



RE: Clearing listboxes - klatlap - Feb-01-2022

cheers, looks like that works, i did something similar but i zip them together, must have been where i went wrong.


RE: Clearing listboxes - deanhystad - Feb-01-2022

You can use winfo_children() if you want to clear all the list boxes. This does entry widgets too.
for child in frame.winfo_children():
    if isinstance(child, (tk.Entry, tk.Listbox):
        child.delete(0, tk.END)