I'm trying to automatically clear all Entry
widgets in a parent widget.
import Tkinter
import re
root=Tkinter.Tk()
E1=Tkinter.Entry(root)
E1.pack()
E2=Tkinter.Entry(root)
E2.pack()
L1=Tkinter.Label(root,text='Label1')
L1.pack()
I'm running into 3 problems
wlist[0]
below is different from the shell output? Eg:
>> wlist=root.winfo_children()
>> wlist
[<Tkinter.Entry instance at 0x00000000151911C8>,
<Tkinter.Entry instance at 0x00000000151BAD88>,
<Tkinter.Label instance at 0x00000000151B29C8>]
>> wlist[0] # shell output
<Tkinter.Entry instance at 0x00000000151911C8>
>> print wlist[0] # print output here is different vs shell output above
.353964488L
Eg
>> re.search(r'Entry',wlist[0])
<< No output >>
Entry
widget, how would you get the widget object itself to perform a delete
method call?Eg:
## Assuming I have a function to to clear the entry
## How would I pass the object from the pattern match in #2 to this function?
def clear_entry(objEntry):
objEntry.delete(0,Tkinter.END)
The items returned from winfo_children()
is a list of widgets. Tkinter widgets have a method to tell you the underlying widget class: winfo_class
.
>>> wlist[0].winfo_class()
'Entry'
You can also simply compare the object type, like you can with any other python object:
>>> isinstance(wlist[0], Tkinter.Entry)
True
Since the result of winfo_children
is a list of widgets, you can iterate over them and clear out all of the entry widgets like this:
for widget in root.winfo_children():
if isinstance(widget, Tkinter.Entry):
widget.delete(0, "end")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With