-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHospital-Management-System.py
More file actions
213 lines (189 loc) · 9.29 KB
/
Hospital-Management-System.py
File metadata and controls
213 lines (189 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import sqlite3
import csv
import os
# Initialize database
def init_db():
if os.path.exists("hospital.db"):
os.remove("hospital.db") # Force schema reset
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS doctors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, specialty TEXT, phone TEXT
)''')
cur.execute('''CREATE TABLE IF NOT EXISTS patients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, age TEXT, gender TEXT, disease TEXT, phone TEXT, admission_date TEXT, room TEXT, doctor_id INTEGER
)''')
cur.execute('''CREATE TABLE IF NOT EXISTS billing (
id INTEGER PRIMARY KEY AUTOINCREMENT,
patient_id INTEGER, total_amount REAL, date TEXT
)''')
conn.commit()
conn.close()
# Main Application
class HospitalApp:
def __init__(self, root):
self.root = root
self.root.title("Hospital Management System - Doctors & Patients")
self.root.geometry("1000x600")
self.root.configure(bg="#f0f8ff")
tab_control = ttk.Notebook(self.root)
self.doctor_tab = tk.Frame(tab_control, bg="#e6f2ff")
self.patient_tab = tk.Frame(tab_control, bg="#e6f2ff")
tab_control.add(self.doctor_tab, text='Doctors')
tab_control.add(self.patient_tab, text='Patients')
tab_control.pack(expand=1, fill="both")
self.setup_doctor_tab()
self.setup_patient_tab()
def setup_doctor_tab(self):
tk.Label(self.doctor_tab, text="Doctor Details", font=("Arial", 16), bg="#e6f2ff").pack(pady=10)
self.doctor_entries = {}
for field in ["Name", "Specialty", "Phone"]:
tk.Label(self.doctor_tab, text=field, bg="#e6f2ff").pack()
entry = tk.Entry(self.doctor_tab)
entry.pack()
self.doctor_entries[field] = entry
tk.Button(self.doctor_tab, text="Save Doctor", bg="#4CAF50", fg="white", command=self.save_doctor).pack(pady=5)
tk.Button(self.doctor_tab, text="Delete Selected", bg="red", fg="white", command=self.delete_doctor).pack(pady=5)
self.doctor_tree = ttk.Treeview(self.doctor_tab, columns=("ID", "Name", "Specialty", "Phone"), show='headings')
for col in self.doctor_tree["columns"]:
self.doctor_tree.heading(col, text=col)
self.doctor_tree.pack(padx=10, pady=10, fill='both', expand=True)
self.doctor_tree.bind("<Double-1>", self.load_doctor_into_form)
self.load_doctors()
def save_doctor(self):
vals = [entry.get() for entry in self.doctor_entries.values()]
if all(vals):
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("INSERT INTO doctors (name, specialty, phone) VALUES (?, ?, ?)", vals)
conn.commit()
conn.close()
self.load_doctors()
messagebox.showinfo("Success", "Doctor record saved")
for e in self.doctor_entries.values(): e.delete(0, tk.END)
else:
messagebox.showerror("Error", "All fields are required")
def delete_doctor(self):
selected = self.doctor_tree.focus()
if selected:
item = self.doctor_tree.item(selected)
doctor_id = item['values'][0]
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("DELETE FROM doctors WHERE id = ?", (doctor_id,))
conn.commit()
conn.close()
self.load_doctors()
messagebox.showinfo("Deleted", "Doctor record deleted")
def load_doctor_into_form(self, event):
selected = self.doctor_tree.focus()
values = self.doctor_tree.item(selected, 'values')
for i, key in enumerate(["Name", "Specialty", "Phone"]):
self.doctor_entries[key].delete(0, tk.END)
self.doctor_entries[key].insert(0, values[i+1])
def load_doctors(self):
for row in self.doctor_tree.get_children():
self.doctor_tree.delete(row)
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("SELECT * FROM doctors")
for row in cur.fetchall():
self.doctor_tree.insert("", tk.END, values=row)
conn.close()
def setup_patient_tab(self):
tk.Label(self.patient_tab, text="Patient Details", font=("Arial", 16), bg="#e6f2ff").pack(pady=10)
self.patient_entries = {}
fields = ["Name", "Age", "Gender", "Disease", "Phone", "Admission Date (YYYY-MM-DD)", "Room", "Assigned Doctor ID"]
for field in fields:
tk.Label(self.patient_tab, text=field, bg="#e6f2ff").pack()
entry = tk.Entry(self.patient_tab)
entry.pack()
self.patient_entries[field] = entry
search_frame = tk.Frame(self.patient_tab, bg="#e6f2ff")
tk.Label(search_frame, text="Search Name:", bg="#e6f2ff").pack(side=tk.LEFT)
self.search_entry = tk.Entry(search_frame)
self.search_entry.pack(side=tk.LEFT, padx=5)
tk.Button(search_frame, text="Search", command=self.search_patient).pack(side=tk.LEFT)
tk.Button(search_frame, text="Export to CSV", command=self.export_patients).pack(side=tk.LEFT, padx=5)
search_frame.pack(pady=5)
tk.Button(self.patient_tab, text="Save Patient", bg="#4CAF50", fg="white", command=self.save_patient).pack(pady=5)
tk.Button(self.patient_tab, text="Delete Selected", bg="red", fg="white", command=self.delete_patient).pack(pady=5)
self.patient_tree = ttk.Treeview(self.patient_tab, columns=("ID", "Name", "Age", "Gender", "Disease", "Phone", "Admission Date", "Room", "Doctor ID"), show='headings')
for col in self.patient_tree["columns"]:
self.patient_tree.heading(col, text=col)
self.patient_tree.pack(padx=10, pady=10, fill='both', expand=True)
self.patient_tree.bind("<Double-1>", self.load_patient_into_form)
self.load_patients()
def save_patient(self):
vals = [entry.get() for entry in self.patient_entries.values()]
if all(vals):
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("INSERT INTO patients (name, age, gender, disease, phone, admission_date, room, doctor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", vals)
conn.commit()
conn.close()
self.load_patients()
messagebox.showinfo("Success", "Patient record saved")
for e in self.patient_entries.values(): e.delete(0, tk.END)
else:
messagebox.showerror("Error", "All fields are required")
def delete_patient(self):
selected = self.patient_tree.focus()
if selected:
item = self.patient_tree.item(selected)
patient_id = item['values'][0]
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("DELETE FROM patients WHERE id = ?", (patient_id,))
conn.commit()
conn.close()
self.load_patients()
messagebox.showinfo("Deleted", "Patient record deleted")
def load_patient_into_form(self, event):
selected = self.patient_tree.focus()
values = self.patient_tree.item(selected, 'values')
for i, key in enumerate(["Name", "Age", "Gender", "Disease", "Phone", "Admission Date (YYYY-MM-DD)", "Room", "Assigned Doctor ID"]):
self.patient_entries[key].delete(0, tk.END)
self.patient_entries[key].insert(0, values[i+1])
def search_patient(self):
query = self.search_entry.get().lower()
for row in self.patient_tree.get_children():
self.patient_tree.delete(row)
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("SELECT * FROM patients WHERE lower(name) LIKE ?", ('%' + query + '%',))
for row in cur.fetchall():
self.patient_tree.insert("", tk.END, values=row)
conn.close()
def export_patients(self):
filename = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV Files", "*.csv")])
if filename:
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("SELECT * FROM patients")
rows = cur.fetchall()
with open(filename, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["ID", "Name", "Age", "Gender", "Disease", "Phone", "Admission Date", "Room", "Doctor ID"])
writer.writerows(rows)
conn.close()
messagebox.showinfo("Exported", "Data exported to CSV")
def load_patients(self):
for row in self.patient_tree.get_children():
self.patient_tree.delete(row)
conn = sqlite3.connect("hospital.db")
cur = conn.cursor()
cur.execute("SELECT * FROM patients")
for row in cur.fetchall():
self.patient_tree.insert("", tk.END, values=row)
conn.close()
# Run app
if __name__ == "__main__":
init_db()
root = tk.Tk()
app = HospitalApp(root)
root.mainloop()