[Tutor] Searching db while typing on Entry widget
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Jan 16 19:08:55 EST 2017
On 16/01/17 15:11, Ali Moradi wrote:
> Hi. i want to search one field of my db while i type text in my Entry
> widget. how can i do that?
Assuming you mean a live search type of thing, like Google etc
do then you need to bind the keypress (or maybe the key-up)
event to a function that does the search based on the current
contents of the entry box.
> and another problem is that now when i click on
> one of the item lists it always writes the first field of my db in the Text
> widget, i think i did the loop wrong maybe but i am not sure. #!
...
>
> def enter_meaning(self, tag):
> if self.listbox.curselection():
> self.cur.execute('SELECT English FROM words')
> for row in self.cur:
> self.cur.fetchall()
I suspect this should be outside the loop?
You presumably only want to fetchall once?
And then you want to loop over the results
you retrieved?
[Alternatively you can loop over the cursor
without the fetchall. I've never actually tried
that approach but it should work... Most of
the examples I've seen use the execute call
as the iteration though:
for row in cur.execute(....):
but iterating over the cursor after execute
might work too. But you should only use one
of those techniques not both.
]
Normally I'd do something like:
cur.execute(...)
for row in cur.fetchall():
process(row)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list