我在连接我正在制作的书店 gui 的前端和后端时遇到问题



到目前为止,我正在努力获得;搜索条目";函数工作,但当我输入作者姓名时,它不会显示我试图搜索的条目,记住,我只是想弄清楚为什么该条目没有显示在Listbox中。这是代码。你能确定一下问题吗?

前端:

from tkinter import *
import backend
window = Tk()

def view_command():
ls1.delete(0,END)
for row in backend.view():
ls1.insert(END, row)
def search_command():
ls1.delete(0,END)
for row in backend.search(title_text.get(),year_text.get(),isbn_text.get()):
ls1.insert(END,row)



#Labels
l1 = Label(window, text="Title")
l1.grid(row=0, column=0)
l2 = Label(window, text="Author")
l2.grid(row=0, column=2)
l3 = Label(window, text="Year")
l3.grid(row=1, column=0)
l4 = Label(window, text="ISBN")
l4 .grid(row=1, column=2)
#Entry boxes
title_text=StringVar()
e1 = Entry(window, textvariable=title_text)
e1.grid(row=0, column=1)
author_text=StringVar()
e2 = Entry(window, textvariable=author_text)
e2.grid(row=0, column=3)
year_text=StringVar()
e3 = Entry(window, textvariable=year_text)
e3.grid(row=1, column=1)
isbn_text=StringVar()
e4 = Entry(window, textvariable=isbn_text)
e4.grid(row=1, column=3)
#listbox
ls1 = Listbox(window, height=6,width=35)
ls1.grid(row=2, column=0, rowspan=6, columnspan=2)
#Scrollbar
scr1 = Scrollbar(window)
scr1.grid(row=2, column=2, rowspan=6)
#Making the scrolbar scroll down the listbox
ls1.configure(yscrollcommand=scr1.set)
scr1.configure(command=ls1.yview)
#Buttons and stuff
b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)

b2 = Button(window, text="Search Entry", width=12,command=search_command)
b2.grid(row=3, column=3)
b3 = Button(window, text="Add Entry", width=12)
b3.grid(row=4, column=3)
b4 = Button(window, text="Update Selected", width=12)
b4.grid(row=5, column=3)
b5 = Button(window, text="Delete Selected", width=12)
b5.grid(row=6, column=3)
b6 = Button(window, text="Exit", width=12)
b6.grid(row=7, column=3)
window.mainloop()

后端:

import sqlite3
def connect():
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)")
con.commit()
con.close()

def insert(title,author,year,isbn):
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title,author,year,isbn))
con.commit()
con.close()

def view():
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("SELECT * FROM book")
rows=cr.fetchall()
con.close()
return rows

def search(title="",author="",year="",isbn=""):
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
rows=cr.fetchall()
con.close()
return rows
def delete(id):
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("DELETE FROM book WHERE id=?",(id,))
con.commit()
con.close() 
def update(id, title, author, year, isbn):
con=sqlite3.connect("books.db")
cr=con.cursor()
cr.execute("UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?",(title,author,year,isbn,id))
con.commit()
con.close()
connect()
#insert("The Sun", "John Smith", 1918, 193123132)
#delete(3)
#zupdate(1, "The Moon", "John Smooth", 1917, 99999999)
#print(view())
print(search(author="John Smooth"))

您只是忘记在前端实现的search_command函数中添加author_text.get()。以下是该功能的外观:

def search_command():
ls1.delete(0,END)
for row in backend.search(title_text.get(),author_text.get(),year_text.get(),isbn_text.get()):
ls1.insert(END,row)

最新更新