要从Entry小部件获取文本,请使用get()方法(它返回文本框中的字符串)。在name_box = Entry()
name_box.pack(side=RIGHT, expand=True)
person_name = name_box.get()
但是,您需要添加一个按钮,正如@abarnert在评论中所说的那样。附加此功能:
^{pr2}$
(因此,修改read_from_db()以获取如下变量:def read_from_db(var):
curs.execute("SELECT *" + " FROM personal WHERE Name LIKE (?)", ('%'+var+'%',))
)
按钮的代码示例如下:button = Button(top, text="Display text", command=get_name)
把它们放在一起: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()