1+ from customtkinter import *
2+ from PIL import Image
3+ from tkinter import ttk ,messagebox
4+ import database
5+ #functions
6+
7+ def delete_employee ():
8+ selected_item = tree .selection ()
9+ if not selected_item :
10+ messagebox .showerror ('Error' , 'select data to delete' )
11+ else :
12+ database .delete (identry .get ())
13+ treeview_data ()
14+ clear ()
15+ messagebox .showinfo ('Success' , 'Employee data is deleted successfully' )
16+ print ('deleted' )
17+ def update_employee ():
18+ selected_items = tree .selection ()
19+ if not selected_items :
20+ messagebox .showerror ('Error' , 'No employee selected' )
21+ else :
22+ database .update (identry .get (), nameentry .get (), phoneentry .get (), rolebox .get (), genderbox .get (), salaryentry .get ())
23+ treeview_data ()
24+ clear ()
25+ messagebox .showinfo ('Success' , 'Employee updated successfully' )
26+ print ('updated' )
27+
28+
29+ def selection (event ):
30+ selected = tree .selection ()
31+ if selected :
32+ row = tree .item (selected )['values' ]
33+ clear ()
34+ identry .insert (0 ,row [0 ])
35+ nameentry .insert (0 ,row [1 ])
36+ phoneentry .insert (0 ,row [2 ])
37+ rolebox .set (row [3 ])
38+ genderbox .set (row [4 ])
39+ salaryentry .insert (0 ,row [5 ])
40+
41+ def clear (value = False ):
42+ if value :
43+ tree .selection_remove (tree .focus ())
44+ identry .delete (0 ,END )
45+ nameentry .delete (0 ,END )
46+ phoneentry .delete (0 ,END )
47+ rolebox .set ('' )
48+ genderbox .set ('Male' )
49+ salaryentry .delete (0 ,END )
50+
51+ def treeview_data ():
52+ employees = database .fetch_employees ()
53+ tree .delete (* tree .get_children ())
54+ for employee in employees :
55+ tree .insert ('' , 'end' , values = employee )
56+
57+
58+ def add_employee ():
59+ if identry .get () == '' or nameentry .get () == '' or phoneentry .get () == '' or rolebox .get () == '' or genderbox .get () == '' or salaryentry .get () == '' :
60+ messagebox .showerror ('ERROR' ,'ALL Fields are required to enter' )
61+ elif database .id_exists (id ):
62+ messagebox .showerror ('ERROR' ,'ID already exists' )
63+ else :
64+ database .insert (identry .get (),nameentry .get (),phoneentry .get (),rolebox .get (),genderbox .get (),salaryentry .get ())
65+ treeview_data ()
66+ clear ()
67+ messagebox .showinfo ('Success' ,'Employee added successfully' )
68+ print ('added' )
69+
70+
71+ def search_employee ():
72+ if searchentry .get () == '' :
73+ messagebox .showerror ('Error' ,'Search field is required' )
74+ elif searchbox .get () == 'by search' : #need to correct this def
75+ messagebox .showerror ('Error' ,'Select a search criteria' )
76+ else :
77+ searched_data = database .search (searchbox .get (),searchentry .get ())
78+ print ( searched_data )
79+
80+ def show_all ():
81+ treeview_data ()
82+ searchentry .delete (0 ,END )
83+ searchbox .set ('by search' )
84+
85+ def delete_all ():
86+ result = messagebox .showinfo ('Info' ,'DO you really want to delete all the data from the database' )
87+ if result :
88+ database .delete_database ()
89+ messagebox .showinfo ('Success' ,'All employee data deleted successfully' )
90+ print ('all deleted' )
91+
92+ window = CTk ()
93+ window .geometry ("1080x780+200+200" )
94+ window .resizable (1 , 0 )
95+ window .title ("Employee Management System" )
96+ window .configure (fg_color = 'black' )
97+ logo = CTkImage (Image .open ('bg.png' ),size = (930 ,158 ))
98+ logoLabel = CTkLabel (window ,image = logo ,text = '' )
99+ logoLabel .grid (row = 0 ,column = 0 ,columnspan = 2 )
100+ leftframe = CTkFrame (window ,fg_color = 'black' )
101+ leftframe .grid (row = 1 ,column = 0 )
102+
103+ idlabel = CTkLabel (leftframe ,text = 'ID' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
104+ idlabel .grid (row = 0 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
105+
106+ identry = CTkEntry (leftframe ,font = ('arial' ,15 ,'bold' ),width = 180 )
107+ identry .grid (row = 0 ,column = 1 )
108+
109+ namelabel = CTkLabel (leftframe ,text = 'NAME' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
110+ namelabel .grid (row = 1 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
111+
112+ nameentry = CTkEntry (leftframe ,font = ('arial' ,15 ,'bold' ),width = 180 )
113+ nameentry .grid (row = 1 ,column = 1 )
114+
115+ phonelabel = CTkLabel (leftframe ,text = 'Ph.No.' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
116+ phonelabel .grid (row = 2 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
117+
118+ phoneentry = CTkEntry (leftframe ,font = ('arial' ,15 ,'bold' ),width = 180 )
119+ phoneentry .grid (row = 2 ,column = 1 )
120+
121+ rolelabel = CTkLabel (leftframe ,text = 'ROLE' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
122+ rolelabel .grid (row = 3 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
123+ role_options = ['WEB DEV' , 'CLOUD COMPUTING' , 'PYTHON DEV' , 'API DEV' ,'NETWORD ENGINEER' ,'SEO' ,'BACK-END DEV' , 'FULL-STACK DEV' , 'DEVOPS' ]
124+ rolebox = CTkComboBox (leftframe ,values = role_options ,width = 180 ,font = ('arial' ,15 ,'bold' ))
125+ rolebox .grid (row = 3 ,column = 1 )
126+
127+ genderlabel = CTkLabel (leftframe ,text = 'GENDER' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
128+ genderlabel .grid (row = 4 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
129+ gender_option = ['Male' ,'Female' ,'OPTIMUS PRIME' , 'MEGATRON' ,'CYBERTRON' ,'ATTACK HELICOPTER' ]
130+ genderbox = CTkComboBox (leftframe ,values = gender_option ,width = 180 ,font = ('arial' ,15 ,'bold' ))
131+ genderbox .grid (row = 4 ,column = 1 )
132+
133+ salarylabel = CTkLabel (leftframe ,text = 'SALARY' ,font = ('arial' ,18 ,'bold' ),text_color = 'white' )
134+ salarylabel .grid (row = 5 ,column = 0 ,padx = 20 ,pady = 15 ,sticky = 'w' )
135+
136+ salaryentry = CTkEntry (leftframe ,font = ('arial' ,15 ,'bold' ),width = 180 )
137+ salaryentry .grid (row = 5 ,column = 1 )
138+
139+ rightframe = CTkFrame (window )
140+ rightframe .grid (row = 1 ,column = 1 )
141+ search_options = ['ID' , 'NAME' , 'PH.NO.' , 'ROLE' , 'GENDER' , 'SALARY' ]
142+ searchbox = CTkComboBox (rightframe ,values = search_options ,state = 'readonly' )
143+ searchbox .grid (row = 0 ,column = 0 )
144+ searchbox .set ('by search' )
145+
146+ searchentry = CTkEntry (rightframe )
147+ searchentry .grid (row = 0 ,column = 1 )
148+
149+ searchbutton = CTkButton (rightframe ,text = 'SEARCH' ,width = 100 ,command = search_employee )
150+ searchbutton .grid (row = 0 ,column = 2 )
151+
152+ showallbutton = CTkButton (rightframe ,text = 'SHOW ALL' ,width = 100 ,command = show_all )
153+ showallbutton .grid (row = 0 ,column = 3 ,pady = 5 )
154+ tree = ttk .Treeview (rightframe ,height = 13 )
155+ tree .grid (row = 1 ,column = 0 ,columnspan = 4 )
156+ tree ['columns' ] = ('ID' ,'NAME' ,'PH.NO.' ,'ROLE' ,'GENDER' ,'SALARY' )
157+ tree .heading ('ID' ,text = 'ID' )
158+ tree .heading ('NAME' ,text = 'NAME' )
159+ tree .heading ('PH.NO.' ,text = 'PH.NO.' )
160+ tree .heading ('ROLE' ,text = 'ROLE' )
161+ tree .heading ('GENDER' ,text = 'GENDER' )
162+ tree .heading ('SALARY' ,text = 'SALARY' )
163+ tree .config (show = 'headings' )
164+ tree .column ('ID' ,width = 70 )
165+ tree .column ('NAME' ,width = 70 )
166+ tree .column ('PH.NO.' ,width = 70 )
167+ tree .column ('ROLE' ,width = 70 )
168+ tree .column ('GENDER' ,width = 70 )
169+ tree .column ('SALARY' ,width = 70 )
170+ stye = ttk .Style ()
171+ stye .configure ('Treeview.heading' ,rowheight = 27 ,font = ('arial' ,10 ,'bold' ))
172+ stye .configure ('Treeview' ,font = ('arial' ,10 ,'bold' ),rowheight = 27 ,background = '#161C30' ,fieldbackground = '#161C30' ,foreground = 'white' )
173+ scrollbar = ttk .Scrollbar (rightframe ,orient = 'vertical' )
174+ scrollbar .grid (row = 1 ,column = 4 ,sticky = 'ns' )
175+ buttonframe = CTkFrame (window )
176+ buttonframe .grid (row = 2 ,column = 0 ,columnspan = 2 )
177+
178+ newbutton = CTkButton (buttonframe ,text = 'NEW EMPLOYEE' ,font = ('arial' ,15 ,'bold' ),width = 160 ,corner_radius = 15 ,command = lambda :clear (True ))
179+ newbutton .grid (row = 0 ,column = 0 ,padx = 5 ,pady = 5 )
180+
181+ addbutton = CTkButton (buttonframe ,text = 'ADD EMPLOYEE' ,font = ('arial' ,15 ,'bold' ),width = 160 ,corner_radius = 15 ,command = add_employee )
182+ addbutton .grid (row = 0 ,column = 1 ,padx = 5 ,pady = 5 )
183+
184+ updatebutton = CTkButton (buttonframe ,text = 'UPDATE EMPLOYEE' ,font = ('arial' ,15 ,'bold' ),width = 160 ,corner_radius = 15 ,command = update_employee )
185+ updatebutton .grid (row = 0 ,column = 2 ,padx = 5 ,pady = 5 )
186+
187+ deletebutton = CTkButton (buttonframe ,text = 'DELETE EMPLOYEE' ,font = ('arial' ,15 ,'bold' ),width = 160 ,corner_radius = 15 ,command = delete_employee )
188+ deletebutton .grid (row = 0 ,column = 3 ,padx = 5 ,pady = 5 )
189+
190+ deleteallbutton = CTkButton (buttonframe ,text = 'DELETE ALL EMPLOYEE' ,font = ('arial' ,15 ,'bold' ),width = 160 ,corner_radius = 15 ,command = delete_all )
191+ deleteallbutton .grid (row = 0 ,column = 4 ,padx = 5 ,pady = 5 )
192+
193+
194+ window .bind ('<ButtonRelease>' ,selection )
195+ treeview_data ()
196+ window .mainloop ()
0 commit comments