Python Forum

Full Version: Clearing listboxes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)
cheers, looks like that works, i did something similar but i zip them together, must have been where i went wrong.
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)