Repository navigation
Expand file tree
/
Copy pathpatient_form.py
More file actions
173 lines (144 loc) · 6 KB
/
Copy pathpatient_form.py
File metadata and controls
173 lines (144 loc) · 6 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
import tkinter as tk
from tkinter import ttk, messagebox
from db_operations import db
class PatientForm:
def __init__(self, on_submit):
self.root = tk.Tk()
self.root.title("Patient Information")
self.root.geometry("400x300")
self.root.resizable(False, False)
self.on_submit = on_submit
# Center the window
window_width = 400
window_height = 300
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
x = (screen_width // 2) - (window_width // 2)
y = (screen_height // 2) - (window_height // 2)
self.root.geometry(f'{window_width}x{window_height}+{x}+{y}')
self.create_widgets()
def create_widgets(self):
# Title
title_label = ttk.Label(
self.root,
text="Patient Login",
font=('Helvetica', 16, 'bold')
)
title_label.pack(pady=10)
# Frame for radio buttons
radio_frame = ttk.LabelFrame(self.root, text="Select Patient Type")
radio_frame.pack(pady=10, padx=20, fill='x')
# Radio buttons for patient type
self.patient_type = tk.StringVar(value='existing')
ttk.Radiobutton(
radio_frame,
text="Existing Patient",
variable=self.patient_type,
value='existing',
command=self.toggle_fields
).pack(anchor='w', pady=5, padx=10)
ttk.Radiobutton(
radio_frame,
text="New Patient",
variable=self.patient_type,
value='new',
command=self.toggle_fields
).pack(anchor='w', pady=5, padx=10)
# Frame for form
self.form_frame = ttk.LabelFrame(self.root, text="Patient Information")
self.form_frame.pack(pady=10, padx=20, fill='x')
# Patient Name (initially hidden)
self.name_label = ttk.Label(self.form_frame, text="Patient Name:")
self.name_label.grid(row=0, column=0, sticky='w', pady=5)
self.name_var = tk.StringVar()
self.name_entry = ttk.Entry(self.form_frame, textvariable=self.name_var, width=30)
self.name_entry.grid(row=0, column=1, sticky='ew', pady=5, padx=5)
# Patient ID
ttk.Label(self.form_frame, text="Patient ID:").grid(row=1, column=0, sticky='w', pady=5)
self.id_var = tk.StringVar()
self.id_entry = ttk.Entry(self.form_frame, textvariable=self.id_var, width=30)
self.id_entry.grid(row=1, column=1, sticky='ew', pady=5, padx=5)
self.id_entry.focus()
# Error message
self.error_var = tk.StringVar()
error_label = ttk.Label(
self.root,
textvariable=self.error_var,
foreground='red',
wraplength=350
)
error_label.pack(pady=5)
# Submit button
submit_btn = ttk.Button(
self.root,
text="Start Game",
command=self.submit,
style='Accent.TButton'
)
submit_btn.pack(pady=10)
# Configure style
style = ttk.Style()
style.configure('Accent.TButton', font=('Helvetica', 10))
# Bind Enter key to submit
self.root.bind('<Return>', lambda e: self.submit())
# Initialize fields based on default selection
self.toggle_fields()
def show(self):
self.root.mainloop()
def toggle_fields(self):
"""Show/hide name field based on patient type"""
if self.patient_type.get() == 'new':
self.name_label.grid()
self.name_entry.grid()
self.form_frame['text'] = "New Patient Information"
else:
self.name_label.grid_remove()
self.name_entry.grid_remove()
self.form_frame['text'] = "Existing Patient Information"
def validate_patient_exists(self, patient_id):
"""Check if patient exists in the database"""
try:
stats = db.get_patient_stats(patient_id)
return stats is not None
except Exception as e:
print(f"Error checking patient: {e}")
return False
def submit(self):
patient_type = self.patient_type.get()
patient_id = self.id_var.get().strip()
if not patient_id:
self.error_var.set("Please enter a Patient ID")
return
if patient_type == 'new':
name = self.name_var.get().strip()
if not name:
self.error_var.set("Please enter a name for the new patient")
return
# Check if patient ID already exists
if self.validate_patient_exists(patient_id):
self.error_var.set(f"Patient ID {patient_id} already exists. Please use a different ID or select 'Existing Patient'.")
return
# Add new patient to database
if not db.add_patient(name, patient_id):
self.error_var.set("Error creating new patient. Please try again.")
return
else: # Existing patient
if not self.validate_patient_exists(patient_id):
self.error_var.set(f"No patient found with ID: {patient_id}")
return
# Get patient name from database
stats = db.get_patient_stats(patient_id)
name = stats.get('name', 'Player')
# Start a new session
if db.start_session(patient_id):
self.root.destroy()
self.on_submit(name, patient_id)
else:
self.error_var.set("Error starting game session. Please try again.")
def get_patient_info(on_submit):
form = PatientForm(on_submit)
form.show()
if __name__ == "__main__":
def test_callback(name, patient_id):
print(f"Starting game for {name} (ID: {patient_id})")
get_patient_info(test_callback)