1
+ from tkinter import *
2
+ import pymysql
3
+ from tkinter import messagebox
4
+ from tkinter import ttk
5
+ from connect_db import connect
6
+ def search_book (home ,search_books ):
7
+ search_books .configure (background = 'black' )
8
+ text = Text (search_books ,height = 1 ,width = 30 ,font = ('Courier' ,25 ))
9
+ text .pack (pady = 15 )
10
+ Button (search_books ,text = 'search' ,command = lambda :book_query (text ,search_books ),bg = 'black' , fg = 'white' , font = ('Courier' ,25 )).pack (pady = 18 )
11
+ search_books .pack (expand = 1 ,fill = X )
12
+ home .pack_forget ()
13
+ return None
14
+ def book_query (text ,search_books ):
15
+ search_text = text .get (1.0 , "end-1c" )
16
+ query = f"select book_id,name,aut_name,cat_name,dept_name,t_count,avail_count from Books natural join (select * from Category natural join Dept) as cat_dept where (lower(name) like lower('%" + search_text + "%'))or(lower(aut_name) like lower('%" + search_text + "%')) or (lower(cat_name) like lower('%" + search_text + "%')) or (lower(dept_name) like lower('%" + search_text + "%'))"
17
+
18
+ try :
19
+ q = connect (query )
20
+ result_refactor (q ,search_books )
21
+ except (pymysql .Error , pymysql .Warning ) as e :
22
+ print (e )
23
+ return None
24
+ def result_refactor (q ,search_books ):
25
+ for i ,widget in enumerate (search_books .winfo_children ()):
26
+ if i == 2 :
27
+ widget .destroy ()
28
+ table = ttk .Treeview (search_books )
29
+ search_result = []
30
+
31
+ for row in q :
32
+ result = {}
33
+ result ["id" ]= row [0 ]
34
+ result ["name" ]= row [1 ]
35
+ result ["author" ]= row [2 ]
36
+ result ["category" ]= row [3 ]
37
+ result ["department" ]= row [4 ]
38
+ result ["t_count" ]= row [5 ]
39
+ result ["avail_count" ]= row [6 ]
40
+ search_result .append (result )
41
+
42
+
43
+ show_result (search_books ,search_result ,table )
44
+
45
+
46
+ def show_result (search_books ,search_result ,table ):
47
+ if len (search_result )== 0 :
48
+ messagebox .showinfo ("oops" ,"no result found" )
49
+
50
+
51
+ table ['columns' ] = ('book_id' , 'book name' , 'author' , 'category' , 'department' ,"total count" ,"available count" )
52
+ table .column ("#0" , width = 0 , stretch = NO )
53
+ table .column ("book_id" ,anchor = CENTER , width = 200 )
54
+ table .column ("book name" ,anchor = CENTER ,width = 300 )
55
+ table .column ("author" ,anchor = CENTER ,width = 200 )
56
+ table .column ("category" ,anchor = CENTER ,width = 200 )
57
+ table .column ("department" ,anchor = CENTER ,width = 200 )
58
+ table .column ("total count" ,anchor = CENTER ,width = 200 )
59
+ table .column ("available count" ,anchor = CENTER ,width = 200 )
60
+
61
+
62
+
63
+ table .heading ("#0" ,text = "" ,anchor = CENTER )
64
+ table .heading ("book_id" ,text = "book_id" ,anchor = CENTER )
65
+ table .heading ("book name" ,text = "book name" ,anchor = CENTER )
66
+ table .heading ("author" ,text = "author" ,anchor = CENTER )
67
+ table .heading ("category" ,text = "category" ,anchor = CENTER )
68
+ table .heading ("department" ,text = "department" ,anchor = CENTER )
69
+ table .heading ("total count" ,text = "total count" ,anchor = CENTER )
70
+ table .heading ("available count" ,text = "available count" ,anchor = CENTER )
71
+ i = 0
72
+ for each in search_result :
73
+ table .insert (parent = '' ,index = 'end' ,iid = i ,text = '' ,
74
+ values = (each ['id' ],each ["name" ],each ["author" ],each ["category" ], each ["department" ],each ["t_count" ],each ["avail_count" ]))
75
+ i += 1
76
+
77
+
78
+ table .pack ()
79
+
80
+ return None
81
+
82
+ # for widget in search_books.winfo_children():
83
+ # widget.destroy()
84
+ # Label(search_books,text="id",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
85
+ # Label(search_books,text=id,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
86
+ # Label(search_books,text="name",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
87
+ # Label(search_books,text=name,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
88
+ # Label(search_books,text="author",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
89
+ # Label(search_books,text=author,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
90
+ # Label(search_books,text="category",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
91
+ # Label(search_books,text=category,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
92
+ # Label(search_books,text="department",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
93
+ # Label(search_books,text=dept,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
94
+ # Label(search_books,text="total count",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
95
+ # Label(search_books,text=t_count,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
96
+ # Label(search_books,text="available count",bg='black', fg='white', font=('Courier',25)).pack(pady=5)
97
+ # Label(search_books,text=avail_count,bg='black', fg='white', font=('Courier',25)).pack(pady=5)
0 commit comments