1010
1111# Load Excel database
1212disease_df = pd .read_excel (os .path .join (script_dir , 'diseases.xlsx' ), sheet_name = 'Disease' )
13- patient_df = pd .read_excel (os .path .join (script_dir , 'database.xlsx' ), sheet_name = 'patients' )
14- doctor_df = pd .read_excel (os .path .join (script_dir , 'database.xlsx' ), sheet_name = 'doctors' )
13+ users_df = pd .read_excel (os .path .join (script_dir , 'database.xlsx' ), sheet_name = 'users' )
1514
1615# Ensure proper column types
17- patient_df ['Diagnosis' ] = patient_df ['Diagnosis' ].astype (str )
18- if 'Appointment Slot' not in patient_df .columns :
19- patient_df ['Appointment Slot' ] = ''
20- if 'Preferred Doctor' not in patient_df .columns :
21- patient_df ['Preferred Doctor' ] = ''
22-
23- # Ensure 'Name' column exists in doctor_df
24- if 'Name' not in doctor_df .columns :
25- doctor_df ['Name' ] = doctor_df ['Username' ] # or another appropriate default value
26-
27- # Ensure 'Specialization' column exists in doctor_df
28- if 'Specialization' not in doctor_df .columns :
29- doctor_df ['Specialization' ] = '' # or another appropriate default value
16+ users_df ['Diagnosis' ] = users_df ['Diagnosis' ].astype (str )
17+ if 'Appointment Slot' not in users_df .columns :
18+ users_df ['Appointment Slot' ] = ''
19+ if 'Preferred Doctor' not in users_df .columns :
20+ users_df ['Preferred Doctor' ] = ''
3021
3122# Main root window
3223root = tk .Tk ()
@@ -77,12 +68,7 @@ def show_login():
7768 password_entry = ttk .Entry (frame , show = '*' )
7869 password_entry .pack (pady = 5 )
7970
80- ttk .Label (frame , text = 'User Type' ).pack (pady = 5 )
81- user_type_var = tk .StringVar (value = 'Patient' )
82- ttk .Radiobutton (frame , text = 'Patient' , variable = user_type_var , value = 'Patient' ).pack (pady = 5 )
83- ttk .Radiobutton (frame , text = 'Doctor' , variable = user_type_var , value = 'Doctor' ).pack (pady = 5 )
84-
85- login_btn = ttk .Button (frame , text = 'Login' , command = lambda : authenticate_user (username_entry , password_entry , user_type_var ))
71+ login_btn = ttk .Button (frame , text = 'Login' , command = lambda : authenticate_user (username_entry , password_entry ))
8672 login_btn .pack (pady = 10 )
8773
8874 signup_btn = ttk .Button (frame , text = 'Sign Up' , command = lambda : show_view ('signup' ))
@@ -104,99 +90,76 @@ def show_signup():
10490 password_entry = ttk .Entry (frame , show = '*' )
10591 password_entry .pack (pady = 5 )
10692
107- ttk .Label (frame , text = 'User Type' ).pack (pady = 5 )
108- user_type_var = tk .StringVar (value = 'Patient' )
109- ttk .Radiobutton (frame , text = 'Patient' , variable = user_type_var , value = 'Patient' ).pack (pady = 5 )
110- ttk .Radiobutton (frame , text = 'Doctor' , variable = user_type_var , value = 'Doctor' ).pack (pady = 5 )
111-
112- specialization_label = ttk .Label (frame , text = 'Specialization' )
113- specialization_entry = ttk .Entry (frame )
114-
115- city_label = ttk .Label (frame , text = 'City' )
116- city_label .pack (pady = 5 )
93+ ttk .Label (frame , text = 'City' ).pack (pady = 5 )
11794 city_entry = ttk .Entry (frame )
11895 city_entry .pack (pady = 5 )
11996
12097 ttk .Label (frame , text = 'Address' ).pack (pady = 5 )
12198 address_entry = ttk .Entry (frame )
12299 address_entry .pack (pady = 5 )
123100
124- def update_specialization_field (* args ):
125- if user_type_var .get () == 'Doctor' :
126- specialization_label .pack (pady = 5 , before = city_label )
127- specialization_entry .pack (pady = 5 , before = city_label )
128- else :
129- specialization_label .pack_forget ()
130- specialization_entry .pack_forget ()
131-
132- user_type_var .trace ('w' , update_specialization_field )
101+ ttk .Label (frame , text = 'Specialization (if Doctor)' ).pack (pady = 5 )
102+ specialization_entry = ttk .Entry (frame )
103+ specialization_entry .pack (pady = 5 )
133104
134- signup_btn = ttk .Button (frame , text = 'Sign Up' , command = lambda : register_user (name_entry , username_entry , password_entry , user_type_var , city_entry , address_entry , specialization_entry ))
105+ signup_btn = ttk .Button (frame , text = 'Sign Up' , command = lambda : register_user (name_entry , username_entry , password_entry , city_entry , address_entry , specialization_entry ))
135106 signup_btn .pack (pady = 10 )
136107
137108 login_btn = ttk .Button (frame , text = 'Back to Login' , command = lambda : show_view ('login' ))
138109 login_btn .pack (pady = 10 )
139110
140- def register_user (name_entry , username_entry , password_entry , user_type_var , city_entry , address_entry , specialization_entry ):
141- global patient_df
142- global doctor_df
111+ def register_user (name_entry , username_entry , password_entry , city_entry , address_entry , specialization_entry ):
112+ global users_df
143113
144114 name = name_entry .get ()
145115 username = username_entry .get ()
146116 password = password_entry .get ()
147- user_type = user_type_var .get ()
148117 city = city_entry .get ()
149118 address = address_entry .get ()
119+ user_type = 'Doctor' if specialization_entry .get () else 'Patient'
150120 specialization = specialization_entry .get () if user_type == 'Doctor' else ''
151121
152122 if not name or not username or not password or not city or not address or (user_type == 'Doctor' and not specialization ):
153123 messagebox .showerror ('Error' , 'All fields are required.' )
154124 return
155125
156- if user_type == 'Patient' :
157- if username in patient_df ['Username' ].values :
158- messagebox .showerror ('Error' , 'Username already exists.' )
159- return
160- new_user = pd .DataFrame ([[name , username , password , '' , '' , city , address ]], columns = ['Name' , 'Username' , 'Password' , 'Diagnosis' , 'Appointment Slot' , 'City' , 'Address' ])
161- patient_df = pd .concat ([patient_df , new_user ], ignore_index = True )
162- elif user_type == 'Doctor' :
163- if username in doctor_df ['Username' ].values :
164- messagebox .showerror ('Error' , 'Username already exists.' )
165- return
166- new_user = pd .DataFrame ([[name , username , password , '' , city , address , specialization ]], columns = ['Name' , 'Username' , 'Password' , 'Assigned Patients' , 'City' , 'Address' , 'Specialization' ])
167- doctor_df = pd .concat ([doctor_df , new_user ], ignore_index = True )
126+ if username in users_df ['Username' ].values :
127+ messagebox .showerror ('Error' , 'Username already exists.' )
128+ return
129+
130+ new_user = pd .DataFrame ([[name , username , password , address , city , '' , '' , '' , user_type , specialization , '' , '' ]],
131+ columns = ['Name' , 'Username' , 'Password' , 'Address' , 'City' , 'Diagnosis' , 'Appointment Slot' , 'Preferred Doctor' , 'User Type' , 'Specialization' , 'Assigned Patients' , 'Doctor ID' ])
132+ users_df = pd .concat ([users_df , new_user ], ignore_index = True )
168133
169134 save_data ()
170135 messagebox .showinfo ('Success' , 'User registered successfully.' )
171136 show_view ('login' )
172137
173- def authenticate_user (username_entry , password_entry , user_type_var ):
138+ def authenticate_user (username_entry , password_entry ):
174139 global current_user
175140 current_user = None # Reset current_user on each login attempt
176141
177142 username = username_entry .get ()
178143 password = password_entry .get ()
179- user_type = user_type_var .get ()
180144
181- if user_type == 'Patient' :
182- user = patient_df [(patient_df ['Username' ] == username ) & (patient_df ['Password' ] == password )]
183- if not user .empty :
184- current_user = username
145+ user = users_df [(users_df ['Username' ] == username ) & (users_df ['Password' ] == password )]
146+ if not user .empty :
147+ current_user = username
148+ user_type = user ['User Type' ].values [0 ]
149+ if user_type == 'Patient' :
185150 show_view ('patient' )
186- else :
187- messagebox .showerror ('Error' , 'Invalid username or password.' )
188- elif user_type == 'Doctor' :
189- doctor = doctor_df [(doctor_df ['Username' ] == username ) & (doctor_df ['Password' ] == password )]
190- if not doctor .empty :
191- current_user = username
151+ elif user_type == 'Doctor' :
192152 show_view ('doctor' )
193- else :
194- messagebox .showerror ('Error' , 'Invalid username or password.' )
153+ else :
154+ messagebox .showerror ('Error' , 'Invalid username or password.' )
195155
196156def show_patient_dashboard ():
197157 global results_frame
198158 frame = ttk .Frame (root , padding = "20" )
199159 frame .pack (fill = 'both' , expand = True )
160+ ttk .Label (frame , text = f'Welcome, { current_user } ' ).pack (pady = 10 )
161+ patient_info = users_df [users_df ['Username' ] == current_user ].iloc [0 ]
162+ ttk .Label (frame , text = f'Name: { patient_info ["Name" ]} ' ).pack (pady = 5 )
200163
201164 # Load and display image
202165 img = Image .open (os .path .join (script_dir , 'patient_dashboard_image.png' ))
@@ -215,6 +178,7 @@ def show_patient_dashboard():
215178
216179 results_frame = ttk .Frame (frame )
217180 results_frame .pack (pady = 10 , fill = 'both' , expand = True )
181+
218182def submit_diagnosis (diagnosis_entry ):
219183 symptoms = [s .strip ().lower () for s in diagnosis_entry .get ().split (',' )]
220184 disease_df ['Symptoms' ] = disease_df ['Symptoms' ].str .strip ().str .lower ()
@@ -228,9 +192,6 @@ def submit_diagnosis(diagnosis_entry):
228192 update_table (results )
229193 save_patient_data ()
230194
231- import tkinter as tk
232- from tkinter import ttk
233-
234195def update_table (results ):
235196 global results_frame
236197 global confirm_btn
@@ -267,25 +228,33 @@ def show_confirm_button(event, tree):
267228 confirm_btn .destroy ()
268229 confirm_btn = ttk .Button (results_frame , text = 'Confirm Selection' , command = lambda : confirm_selection (tree ))
269230 confirm_btn .pack (pady = 10 , side = 'right' , anchor = 'n' ) # Ensure the button is anchored to the top right
231+
270232def confirm_selection (tree ):
271- global patient_df
272233 selected_item = tree .selection ()
273234 if selected_item :
274235 selected_disease = tree .item (selected_item [0 ], 'values' )[0 ]
275- patient_df .loc [patient_df ['Username' ] == current_user , 'Diagnosis' ] = selected_disease
276- save_patient_data ()
236+ users_df .loc [users_df ['Username' ] == current_user , 'Diagnosis' ] = selected_disease
237+ save_data ()
277238 messagebox .showinfo ('Success' , f'Diagnosis "{ selected_disease } " has been added to your record.' )
278239 show_view ('select_doctor' )
279240
280241def save_patient_data ():
281242 try :
282243 with pd .ExcelWriter (os .path .join (script_dir , 'database.xlsx' ), engine = 'openpyxl' , mode = 'a' , if_sheet_exists = 'replace' ) as writer :
283- patient_df .to_excel (writer , sheet_name = 'patients ' , index = False )
244+ users_df .to_excel (writer , sheet_name = 'users ' , index = False )
284245 except FileNotFoundError :
285246 messagebox .showerror ('Error' , 'Database file not found. Please check the file path.' )
286247 except Exception as e :
287248 messagebox .showerror ('Error' , f'An error occurred: { e } ' )
288249
250+ def save_data ():
251+ try :
252+ with pd .ExcelWriter (os .path .join (script_dir , 'database.xlsx' ), engine = 'openpyxl' , mode = 'a' , if_sheet_exists = 'replace' ) as writer :
253+ users_df .to_excel (writer , sheet_name = 'users' , index = False )
254+ except FileNotFoundError :
255+ messagebox .showerror ('Error' , 'Database file not found. Please check the file path.' )
256+ except Exception as e :
257+ messagebox .showerror ('Error' , f'An error occurred: { e } ' )
289258
290259def show_select_doctor ():
291260 frame = ttk .Frame (root , padding = "20" )
@@ -294,7 +263,7 @@ def show_select_doctor():
294263 ttk .Label (frame , text = 'Select Preferred Doctor:' ).pack (pady = 10 )
295264
296265 # Get the city of the current patient
297- patient_city = patient_df .loc [patient_df ['Username' ] == current_user , 'City' ].values [0 ]
266+ patient_city = users_df .loc [users_df ['Username' ] == current_user , 'City' ].values [0 ]
298267
299268 columns = ('Name' , 'Specialization' , 'Address' )
300269 tree = ttk .Treeview (frame , columns = columns , show = 'headings' )
@@ -303,7 +272,7 @@ def show_select_doctor():
303272 tree .column (col , width = 200 , anchor = 'w' )
304273
305274 # Filter doctors based on the patient's city
306- for _ , row in doctor_df [ doctor_df ['City' ] == patient_city ].iterrows ():
275+ for _ , row in users_df [( users_df ['City' ] == patient_city ) & ( users_df [ 'User Type' ] == 'Doctor' ) ].iterrows ():
307276 name = row ['Name' ]
308277 specialization = textwrap .fill (str (row ['Specialization' ]), width = 30 )
309278 address = textwrap .fill (str (row ['Address' ]), width = 30 )
@@ -335,9 +304,6 @@ def show_select_doctor():
335304 schedule_btn .pack (pady = 10 )
336305
337306def schedule_appointment (tree , slot_var ):
338- global patient_df
339- global doctor_df
340-
341307 selected_item = tree .selection ()
342308 if not selected_item :
343309 messagebox .showerror ('Error' , 'Please select a doctor.' )
@@ -347,14 +313,14 @@ def schedule_appointment(tree, slot_var):
347313 selected_slot = slot_var .get ()
348314
349315 # Check if the selected doctor exists in the DataFrame
350- doctor_row = doctor_df . loc [ doctor_df ['Name' ] == selected_doctor ]
316+ doctor_row = users_df [( users_df ['Name' ] == selected_doctor ) & ( users_df [ 'User Type' ] == 'Doctor' ) ]
351317 if doctor_row .empty :
352318 messagebox .showerror ('Error' , 'Selected doctor not found.' )
353319 return
354320
355321 doctor_id = doctor_row ['Username' ].values [0 ]
356- patient_df .loc [patient_df ['Username' ] == current_user , 'Appointment Slot' ] = str (selected_slot )
357- patient_df .loc [patient_df ['Username' ] == current_user , 'Preferred Doctor' ] = doctor_id
322+ users_df .loc [users_df ['Username' ] == current_user , 'Appointment Slot' ] = str (selected_slot )
323+ users_df .loc [users_df ['Username' ] == current_user , 'Preferred Doctor' ] = doctor_id
358324
359325 assigned_patients = doctor_row ['Assigned Patients' ].values [0 ]
360326 if pd .isnull (assigned_patients ):
@@ -363,7 +329,7 @@ def schedule_appointment(tree, slot_var):
363329 assigned_patients = assigned_patients .split (', ' )
364330
365331 assigned_patients .append (current_user )
366- doctor_df .loc [doctor_df ['Username' ] == doctor_id , 'Assigned Patients' ] = ', ' .join (assigned_patients )
332+ users_df .loc [users_df ['Username' ] == doctor_id , 'Assigned Patients' ] = ', ' .join (assigned_patients )
367333
368334 save_data ()
369335 messagebox .showinfo ('Appointment' , f'Appointment scheduled with Dr. { selected_doctor } at { selected_slot } ' )
@@ -374,6 +340,7 @@ def schedule_appointment(tree, slot_var):
374340
375341 # Redirect to login view
376342 show_view ('login' )
343+
377344def show_doctor_dashboard ():
378345 global action_frame
379346 frame = ttk .Frame (root , padding = "20" )
@@ -390,7 +357,7 @@ def show_doctor_dashboard():
390357 back_btn .pack (pady = 10 )
391358
392359def load_patient_data (username , frame ):
393- doc_info = doctor_df [ doctor_df ['Username' ] == username ]
360+ doc_info = users_df [( users_df ['Username' ] == username ) & ( users_df [ 'User Type' ] == 'Doctor' ) ]
394361 if doc_info .empty :
395362 messagebox .showerror ('Error' , 'No doctor data found for the username.' )
396363 return
@@ -413,7 +380,7 @@ def load_patient_data(username, frame):
413380 tree .column (col , width = 150 )
414381
415382 for patient in assigned_patients :
416- patient_data = patient_df [ patient_df ['Username' ] == patient ]
383+ patient_data = users_df [ users_df ['Username' ] == patient ]
417384 if not patient_data .empty :
418385 tree .insert ('' , 'end' , values = (patient_data ['Name' ].values [0 ], patient_data ['Diagnosis' ].values [0 ], patient_data ['Appointment Slot' ].values [0 ], patient_data ['Preferred Doctor' ].values [0 ]))
419386
@@ -430,7 +397,7 @@ def show_action_buttons(event, tree, username, assigned_patients):
430397 selected_item = tree .selection ()
431398 if selected_item :
432399 patient_name = tree .item (selected_item [0 ], 'values' )[0 ]
433- patient_username = patient_df [ patient_df ['Name' ] == patient_name ]['Username' ].values [0 ]
400+ patient_username = users_df [ users_df ['Name' ] == patient_name ]['Username' ].values [0 ]
434401
435402 # Remove existing buttons if any
436403 for widget in action_frame .winfo_children ():
@@ -443,13 +410,10 @@ def show_action_buttons(event, tree, username, assigned_patients):
443410 btn_progress .pack (pady = 5 )
444411
445412def mark_done (patient_username , username , assigned_patients ):
446- global patient_df
447- global doctor_df
448-
449413 if patient_username in assigned_patients :
450414 assigned_patients .remove (patient_username )
451- doctor_df .loc [doctor_df ['Username' ] == username , 'Assigned Patients' ] = ', ' .join (assigned_patients )
452- patient_df .loc [patient_df ['Username' ] == patient_username , 'Preferred Doctor' ] = ''
415+ users_df .loc [users_df ['Username' ] == username , 'Assigned Patients' ] = ', ' .join (assigned_patients )
416+ users_df .loc [users_df ['Username' ] == patient_username , 'Preferred Doctor' ] = ''
453417 save_data ()
454418 show_view ('doctor' )
455419 else :
@@ -458,21 +422,11 @@ def mark_done(patient_username, username, assigned_patients):
458422def mark_in_progress (patient_username , assigned_patients ):
459423 next_patient = assigned_patients [0 ] if assigned_patients else None
460424 if next_patient :
461- next_patient_name = patient_df [ patient_df ['Username' ] == next_patient ]['Name' ].values [0 ]
425+ next_patient_name = users_df [ users_df ['Username' ] == next_patient ]['Name' ].values [0 ]
462426 messagebox .showinfo ('Next Patient' , f'Next patient: { next_patient_name } ' )
463427 else :
464428 messagebox .showinfo ('Next Patient' , 'No more patients in the schedule.' )
465429
466- def save_data ():
467- try :
468- with pd .ExcelWriter (os .path .join (script_dir , 'database.xlsx' ), engine = 'openpyxl' , mode = 'a' , if_sheet_exists = 'replace' ) as writer :
469- patient_df .to_excel (writer , sheet_name = 'patients' , index = False )
470- doctor_df .to_excel (writer , sheet_name = 'doctors' , index = False )
471- except FileNotFoundError :
472- messagebox .showerror ('Error' , 'Database file not found. Please check the file path.' )
473- except Exception as e :
474- messagebox .showerror ('Error' , f'An error occurred: { e } ' )
475-
476430# Show login view initially
477431show_view ('login' )
478432
0 commit comments