Skip to content
Advertisement

(Python) Is there a way to clear all entry boxes in a Tkinter UI in one line?

I have made a Tkinter UI for a fairly simple calculator that all works fine, and I have a clear button to clear all entry boxes. I’d rather have one line of code to clear all 40+ boxes than 40+ lines to clear each individually, and was wondering if there was a Tkinter command that could do the same as ent.delete(0, END) but for all entries?

Advertisement

Answer

This should be sufficient:

JavaScript

It uses list comprehension to loop through all the children of root and does .delete() if the child is an Entry

Implementation:

JavaScript

NOTE:
I should point out that this is probably not best practice but I dont really know.
It creates a list but doesnt assign it to anything so essentially it just does what you intended

EDIT:
Also consider the following if you want to clear every entry, even if it is inside a different widget. This is to be used only if you are sure that every single entry in the whole application should be cleared

JavaScript

Also note that, I used a normal for loop for this, because as noted in the comments, it is better

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement