Python Forum
performs the search without pressing enter or a key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
performs the search without pressing enter or a key
#1
i need help, I want to create a python search engine that will search for clients' names (from a sqlite3 database and display them on the Treeview object),search automatically without clicking on search Button (i.e. in real time). The code below allows a good search but the problem is that it doesn't take into account the last letter typed. example if I write in the search field 'Nam' it executes the search for 'Na' just if I write in the search field 'Name' it performs the search for 'Nam' just (i.e. either press enter or a key so that it performs the search)

 def SearchByName(event):
    for x in Tree.get_children():
        Tree.delete(x)
    Name = entrySearchByName.get()
    conn = sqlite3.connect('database.db')
    cur = conn.cursor()
    SQL = "SELECT*FROM customers where Name LIKE ?"
    Wc_Name = "%{}%".format(Name)
    select = cur.execute(SQL, (Wc_Name,))
    select =list(select)
    conn.commit()
    for row in select:
        Tree.insert('', END, values = row)
        conn.close()
 
lbSearchByName = Label(root, text="Search By Name :", font=("Times New Roman",18))
lbSearchByName.place(x=540, y=2)
entrySearchByName = Entry(root)
entrySearchByName.place(x=730, y=7, width=250, height=25)
entrySearchByName.bind("<Key>", SearchByName)
Reply
#2
Maybe bind to Return instead of Key?
Reply
#3
how can i use after() function to call the function with a slight delay
the insertion will have been handled, and entrySearchByName.get()
will return the full contents.


I want to create a Python search engine that will search for clients' names (from a sqlite3 database and display them on the Treeview object), search automatically without clicking on search Button (i.e. in real time). The code below allows a good search, but the problem is that it doesn't take into account the last letter typed.

example:

if I write in the search field 'Nam' it executes the search for 'Na' only. If I write in the search field 'Name' it performs the search for 'Nam' only (i.e. either press enter or a key so that it performs the search)

 def SearchByName(event):
    for x in Tree.get_children():
        Tree.delete(x)
    Name = entrySearchByName.get()
    conn = sqlite3.connect('database.db')
    cur = conn.cursor()
    SQL = "SELECT*FROM customers where Name LIKE ?"
    Wc_Name = "%{}%".format(Name)
    select = cur.execute(SQL, (Wc_Name,))
    select =list(select)
    conn.commit()
    for row in select:
        Tree.insert('', END, values = row)
        conn.close()
 
lbSearchByName = Label(root, text="Search By Name :", font=("Times New Roman",18))
lbSearchByName.place(x=540, y=2)
entrySearchByName = Entry(root)
entrySearchByName.place(x=730, y=7, width=250, height=25)
entrySearchByName.bind("<Key>", SearchByName) 
Reply
#4
I misunderstood your OP and thought you wanted to wait until all text was entered before starting search. Instead you want to search all the text that is in the entry field.

As you have discovered, this is difficult to do binding to the key event. The key event is really meant to be used to intercept a key press before it is processed. You want to call something after the key press is processed. To do that I suggest you use "trace".
from tkinter import *

def search(var, index,  mode):
    label_text.set(entry_text.get())

root = Tk()
root.geometry('220x80')

label_text = StringVar()
label = Label(root, textvar=label_text)
label.place(x=10, y=10, width=200)

entry_text = StringVar()
entry_text.trace_add('write', search)
entry = Entry(root, textvar = entry_text)
entry.place(x=10, y=50, width=200)
In this example the search() function is called when the "entry_text" variable is written. This happens after the key press event and after the latest key is added to the entry text.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Build a matrix by pressing buttons of an interface in Tkinter which extract data from juandiegopulla 1 1,897 Sep-13-2021, 07:28 PM
Last Post: deanhystad
  Pressing non-latin characters? Murlog 0 1,502 Jul-25-2020, 03:10 PM
Last Post: Murlog
  Error on pressing enter dake 6 4,653 Feb-04-2018, 04:23 PM
Last Post: dake
  Py2EXE: terminal window closes after pressing ENTER peanutbutterjelly 1 5,052 May-06-2017, 07:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020