'How to make a multi search for GUI in python
I want to make a multi search that when i choose a selection value from a combo box, it will search the corresponding value. Here is my code:
search = Tk()
search.title("Search")
search.geometry("300x300")
drop = ttk.Combobox(search, value=["Search by...", "Book ID", "Book Name", "Author
Name", "Genre"])
drop.current(0)
drop.place(x=300, y=10)
def search_info():
global id_entry
global book_name_entry
global author_name_entry
global genre_entry
global myresult
id_get = id_entry.get()
book_name_get = book_name_entry.get()
author_name_get = author_name_entry.get()
genre_get = genre_entry.get()
selected = drop.get()
sql = ""
if selected == ["Search by..."]:
test = Label(search, text="Tf u want to search?")
test.place(x=300, y=70)
if selected == ["Book ID"]:
sql = "SELECT id,book_name,author_name,genre,price FROM books where id = '" +
id_get + "'"
if selected == ["Book Name"]:
sql = "SELECT id,book_name,author_name,genre,price FROM books where book_name =
'" + book_name_get + "'"
if selected == ["Author Name"]:
sql = "SELECT id,book_name,author_name,genre,price FROM books where author_name
= '" + author_name_get + "'"
if selected == ["Genre"]:
sql = "SELECT id,book_name,author_name,genre,price FROM books where genre = '" +
genre_get + "'"
mysqldb = mysql.connector.connect(host="Localhost", user="root",password="12345678",
database="bookstore")
mycursor = mysqldb.cursor()
records = mycursor.execute(sql)
records = mycursor.fetchall()
print(records)
for i, (id1,book_name1,author_name1, genre1,price1) in enumerate(records,start=1):
listBox.insert("", "end", values=(id1,book_name1,author_name1, genre1,price1))
mysqldb.close()
Label(search, text="Search").place(x=10, y=10)
Button(search, text="Search", command=search_info ,height = 1, width = 13).place(x=140,
y=40)
sql = Entry(search)
sql.place(x=140, y=10)
cols = ('id', 'book_name', 'author_name','genre','price')
listBox = ttk.Treeview(search, columns=cols, show='headings' )
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
listBox.place(x=10, y=200)
search.mainloop()
But when i try, it show that: NameError: name 'id_entry' is not defined Now i still sticky with this problem so i hope someone can help me to fix problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
