I want to use Tkinter to create a search form where the user can input the name they want to see from the SQLite3 database. A database named New_Assignment has all the details about the person. But I am confused about how to connect the Tkinter to the database and use the name to search? This is what I have got so far.
import sqlite3
conn = sqlite3.connect('new_assignment.db')
c = conn.cursor()
from tkinter import *
top = Tk()
top.title('Search form')
person_name = Entry()
person_name.pack(side = RIGHT, expand = True)
mainloop()
To get the text from the Entry
widget, use the get()
method (it returns the string in the textbox).
name_box = Entry()
name_box.pack(side=RIGHT, expand=True)
person_name = name_box.get()
However, you will need to add a button as @abarnert says in the comments. Attach this function to it:
def get_name():
global person_name
person_name = name_box.get()
data = c.fetchall()
print(data)
for row in data:
searchlist.append(row)
var1=str(person_name)
read_from_db(var1)
(Of couse, modify read_from_db()
to take a variable like so:
def read_from_db(var):
curs.execute("SELECT *" + " FROM personal WHERE Name LIKE (?)", ('%'+var+'%',))
)
An example of the code for a button might look like this:
button = Button(top, text="Display text", command=get_name)
Place it all together:
import sqlite3
conn = sqlite3.connect('new_assignment.db')
c = conn.cursor()
from tkinter import *
top = Tk()
top.title('Search form')
name_box = Entry()
name_box.pack(side = RIGHT, expand = True)
def get_name():
global person_name
person_name = name_box.get()
data = c.fetchall()
print(data)
for row in data:
searchlist.append(row)
var1=str(person_name)
read_from_db(var1)
def read_from_db(var):
curs.execute("SELECT *" + " FROM personal WHERE Name LIKE (?)", ('%'+var+'%',))
person_name = ""
button = Button(top, text="Display text", command=get_name)
button.pack()
mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.