Hospital Management System using Python

This project involves building a Hospital Management System using Python, Tkinter for the GUI, and SQLite for managing the hospital’s data. The system will handle patient details, doctor appointments, and other hospital-related records. This project is ideal for tracking patient information, managing appointments, and simplifying hospital workflows.

1. Project Setup

Modules Required:

  • tkinter: For creating the GUI.
  • sqlite3: For database management.

Install the necessary modules using:

pip install tkinter

2. Project Features

  1. Add Patient Details: Add new patient information such as name, age, gender, and health issue.
  2. Add Doctor Information: Add and manage doctor details such as name, specialization, and availability.
  3. Book Appointments: Schedule appointments between patients and doctors.
  4. View All Patients: Display all patient records.
  5. View All Doctors: View doctor details and availability.
  6. Search Patient: Search for a patient using their name or ID.
  7. Search Doctor: Find a doctor by specialization or name.
  8. Manage Appointments: Track, update, and delete appointments.

3. Database Design

We’ll use an SQLite database with three tables: patients, doctors, and appointments.

  • patients:
  • id (INTEGER PRIMARY KEY AUTOINCREMENT)
  • name (TEXT)
  • age (INTEGER)
  • gender (TEXT)
  • issue (TEXT)
  • doctors:
  • id (INTEGER PRIMARY KEY AUTOINCREMENT)
  • name (TEXT)
  • specialization (TEXT)
  • availability (TEXT)
  • appointments:
  • id (INTEGER PRIMARY KEY AUTOINCREMENT)
  • patient_id (INTEGER)
  • doctor_id (INTEGER)
  • appointment_time (TEXT)

4. Code Structure

A. Database Connection

import sqlite3

def connect_db():
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    # Creating Patients Table
    c.execute('''CREATE TABLE IF NOT EXISTS patients
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  name TEXT,
                  age INTEGER,
                  gender TEXT,
                  issue TEXT)''')
    # Creating Doctors Table
    c.execute('''CREATE TABLE IF NOT EXISTS doctors
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  name TEXT,
                  specialization TEXT,
                  availability TEXT)''')
    # Creating Appointments Table
    c.execute('''CREATE TABLE IF NOT EXISTS appointments
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  patient_id INTEGER,
                  doctor_id INTEGER,
                  appointment_time TEXT)''')
    conn.commit()
    conn.close()

connect_db()

B. Add Patient Function

def add_patient(name, age, gender, issue):
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    c.execute("INSERT INTO patients (name, age, gender, issue) VALUES (?, ?, ?, ?)",
              (name, age, gender, issue))
    conn.commit()
    conn.close()

C. Add Doctor Function

def add_doctor(name, specialization, availability):
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    c.execute("INSERT INTO doctors (name, specialization, availability) VALUES (?, ?, ?)",
              (name, specialization, availability))
    conn.commit()
    conn.close()

D. Book Appointment Function

def book_appointment(patient_id, doctor_id, appointment_time):
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    c.execute("INSERT INTO appointments (patient_id, doctor_id, appointment_time) VALUES (?, ?, ?)",
              (patient_id, doctor_id, appointment_time))
    conn.commit()
    conn.close()

E. View All Patients Function

def view_patients():
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    c.execute("SELECT * FROM patients")
    rows = c.fetchall()
    conn.close()
    return rows

F. View All Doctors Function

def view_doctors():
    conn = sqlite3.connect('hospital_management.db')
    c = conn.cursor()
    c.execute("SELECT * FROM doctors")
    rows = c.fetchall()
    conn.close()
    return rows

5. GUI Design using Tkinter

Now, let’s build a simple user interface using Tkinter for adding and viewing patients, doctors, and appointments.

from tkinter import *
from tkinter import messagebox
import sqlite3

# Main window setup
root = Tk()
root.title("Hospital Management System")
root.geometry("600x400")

# Function to Add Patient from GUI
def add_patient_gui():
    name = name_entry.get()
    age = age_entry.get()
    gender = gender_entry.get()
    issue = issue_entry.get()

    if name and age and gender and issue:
        add_patient(name, int(age), gender, issue)
        messagebox.showinfo("Success", "Patient added successfully!")
    else:
        messagebox.showerror("Error", "Please fill in all the fields!")

# GUI Elements for Adding Patient
Label(root, text="Patient Name").grid(row=0, column=0, padx=20, pady=10)
name_entry = Entry(root)
name_entry.grid(row=0, column=1)

Label(root, text="Age").grid(row=1, column=0, padx=20, pady=10)
age_entry = Entry(root)
age_entry.grid(row=1, column=1)

Label(root, text="Gender").grid(row=2, column=0, padx=20, pady=10)
gender_entry = Entry(root)
gender_entry.grid(row=2, column=1)

Label(root, text="Health Issue").grid(row=3, column=0, padx=20, pady=10)
issue_entry = Entry(root)
issue_entry.grid(row=3, column=1)

Button(root, text="Add Patient", command=add_patient_gui).grid(row=4, column=0, columnspan=2, pady=20)

root.mainloop()

6. Final Enhancements

To make the Hospital Management System more comprehensive, you can add the following features:

  1. Appointment Management: A system to allow the hospital staff to assign doctors to patients based on availability.
  2. Patient Record Search: Search patient records by name, age, or health issue.
  3. Doctor Search: Search for doctors by name or specialization.
  4. Appointment Rescheduling and Deletion: Add the ability to reschedule or delete appointments.
  5. Improved User Interface: Organize the layout using frames and provide more navigation options.

7. Conclusion

This is a basic Hospital Management System built using Python and Tkinter for the graphical interface, and SQLite for storing hospital data. The system allows hospital staff to manage patient records, doctor information, and appointments efficiently.

Would you like to add any specific features or improve the user interface?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *