-1

Am trying to query from sqlite3 db without clicking on the button with the command.So i did event binding to achieve that but if i have to query it i have to click keybaord enter key and also i run the function query_record() when the program starts to achieve this but not able to achieve the result.

I want the record to appear in the Listbox as soon i enter it in the entry widget without clicking on the button or keyboard enter key

I know it has to be kind of event binding to achieve this but don't know how to achieve that, your suggestions are welcome to achieve this.

import tkinter as tk
import sqlite3

conn = sqlite3.connect("STATS.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS institution(id INTEGER PRIMARY KEY, 
name TEXT)")
conn.commit()
conn.close()


root = tk.Tk()
root.geometry("300x300")

List = tk.Listbox(root, width=100)
List.pack()

e1_search = tk.StringVar()
e1 = tk.Entry(root, textvariable=e1_search)
e1.pack()


def query_record(event=None):
    data1 = e1_search.get()

    conn = sqlite3.connect("STATS.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM institution WHERE name like ?", (data1+"%",))
    row = cur.fetchall()

    for n in row:
        List.delete(0, tk.END)
        List.insert(tk.END, n)
        print(n)
    conn.close()

query_record()

e1.bind("<Return>", query_record)

b = tk.Button(text="Search", command=query_record)
b.pack(side=tk.BOTTOM)

root.mainloop()
O JOE
  • 587
  • 2
  • 17
  • 31
  • How will the computer know when to run the query? If you type "a", does it do the query? What about when you type "ab", does it run the query again? Is that what you're asking -- how to re-run the query on every keystroke? – Bryan Oakley Feb 23 '18 at 15:33
  • @BryanOakley when i enter `a` or `ab` in the entry it doesn't run the `querry` unless i click on the enter key or click the button – O JOE Feb 23 '18 at 15:38
  • i want the query to be run when i type in the `entry` widget – O JOE Feb 23 '18 at 15:40
  • i want it be run on every keystroke – O JOE Feb 23 '18 at 15:40

1 Answers1

0

The simplest solution to have a function called every time the user types a character is to bind on <Any-KeyRelease>. Like the name implies, the function will be called every time the user presses and then releases any key.

e1.bind("<Any-KeyRelease>", query_record)

Another way would be to add a trace on the variable associated with the entry. The advantage to this is that it would work for other cases, such as a user pasting into the field with the mouse, or other code in your program setting the variable to an explicit value. Plus, it would not be called when the user presses a key such as the arrow keys.

def query_record(*args):
    ...
e1_search.trace("w", query_record)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • please can you link me to site which explain this `e1_search.trace("w"` in know i can any alphabet for this see error when i change `_tkinter.TclError: bad operations "q": should be one or more of rwua` – O JOE Feb 23 '18 at 16:48
  • @OJOE: here are some links: http://effbot.org/tkinterbook/variable.htm, https://stackoverflow.com/q/29690463/7432. You can't just use any letter in that first argument. The "w" stands for "write". See https://stackoverflow.com/q/29690463/7432 for more information. – Bryan Oakley Feb 23 '18 at 16:54