[Tutor] I get an error while search in my Entry field
Peter Otten
__peter__ at web.de
Mon Jun 18 05:22:21 EDT 2018
Alan Gauld via Tutor wrote:
> On 17/06/18 23:59, Ali M wrote:
>
>> def update_list(self):
>> search_term = self.search_var.get()
>> self.listbox.delete(0, tk.END)
>> for item in self.listbox:
>
> The above pair of lines look odd.
> You delete everything on the listbox then try to iterate
> over it? Is that really what you want?
More likely the words have to be reread from the database.
> Also are you sure you can iterate over a listbox widget?
> I was not aware of that capability and can see nothing
> about iterators in the listbox help screen...
You cannot iterate over Listbox widgets, it's just that for every object
with a __getitem__() method Python will try and feed it consecutive integers
>>> class A:
... def __getitem__(self, index):
... if index > 3: raise IndexError
... return index * index
...
>>> list(A())
[0, 1, 4, 9]
a feature that probably predates the iterator protocol.
However, the tkinter widgets expect option names like "background", or
"borderstyle", and respond with the somewhat cryptic TypeError when the 0 is
passed instead.
> I usually use the get() method to fetch the contents
> before trying to iterate over them:
>
> for item in myList.get(0,tk.END):
> # use item
>
More information about the Tutor
mailing list