pycharm编写病人管理系统
时间: 2023-11-01 08:53:47 浏览: 83
病人管理系统
5星 · 资源好评率100%
首先,你需要安装PyCharm IDE。然后,你可以按照以下步骤编写一个基本的病人管理系统:
1. 创建一个新项目并命名为“Patient Management System”。
2. 在项目中创建一个Python文件,命名为“patient_management.py”。
3. 引入必要的库,例如Tkinter用于GUI界面设计、SQLite用于数据库管理等。
4. 创建一个GUI窗口并添加所需的文本框、标签和按钮,设计一个用户友好的界面。
5. 创建一个SQLite数据库并设计一个表格来存储病人的个人信息。定义插入、删除、更新和查询数据的函数。
6. 将GUI界面中的文本框和按钮与相应的函数链接起来,使用户能够执行所需的操作。
7. 运行程序并测试它是否按预期工作。如果有错误,请修复它们。
下面是一个示例代码,可供你参考:
```python
import tkinter as tk
import sqlite3
def connect_db():
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS patients (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
gender TEXT,
phone TEXT,
email TEXT
)''')
conn.commit()
conn.close()
def add_patient(name, age, gender, phone, email):
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''INSERT INTO patients (name, age, gender, phone, email)
VALUES (?, ?, ?, ?, ?)''', (name, age, gender, phone, email))
conn.commit()
conn.close()
def delete_patient(id):
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''DELETE FROM patients WHERE id = ?''', (id,))
conn.commit()
conn.close()
def update_patient(id, name, age, gender, phone, email):
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''UPDATE patients SET name = ?, age = ?, gender = ?, phone = ?, email = ?
WHERE id = ?''', (name, age, gender, phone, email, id))
conn.commit()
conn.close()
def search_patient(name):
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''SELECT * FROM patients WHERE name LIKE ?''', ('%' + name + '%',))
results = c.fetchall()
conn.close()
return results
def view_all():
conn = sqlite3.connect('patients.db')
c = conn.cursor()
c.execute('''SELECT * FROM patients''')
results = c.fetchall()
conn.close()
return results
def clear_fields():
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
def add_patient_command():
add_patient(name_entry.get(), age_entry.get(), gender_entry.get(), phone_entry.get(), email_entry.get())
clear_fields()
def delete_patient_command():
delete_patient(id_entry.get())
clear_fields()
def update_patient_command():
update_patient(id_entry.get(), name_entry.get(), age_entry.get(), gender_entry.get(), phone_entry.get(), email_entry.get())
clear_fields()
def search_patient_command():
results = search_patient(name_entry.get())
listbox.delete(0, tk.END)
for row in results:
listbox.insert(tk.END, row)
def view_all_command():
results = view_all()
listbox.delete(0, tk.END)
for row in results:
listbox.insert(tk.END, row)
# GUI界面设计
window = tk.Tk()
window.title('Patient Management System')
name_label = tk.Label(window, text='Name')
name_label.grid(row=0, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
age_label = tk.Label(window, text='Age')
age_label.grid(row=1, column=0)
age_entry = tk.Entry(window)
age_entry.grid(row=1, column=1)
gender_label = tk.Label(window, text='Gender')
gender_label.grid(row=2, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1)
phone_label = tk.Label(window, text='Phone')
phone_label.grid(row=3, column=0)
phone_entry = tk.Entry(window)
phone_entry.grid(row=3, column=1)
email_label = tk.Label(window, text='Email')
email_label.grid(row=4, column=0)
email_entry = tk.Entry(window)
email_entry.grid(row=4, column=1)
id_label = tk.Label(window, text='ID (for deletion and update only)')
id_label.grid(row=5, column=0)
id_entry = tk.Entry(window)
id_entry.grid(row=5, column=1)
listbox = tk.Listbox(window, height=10, width=50)
listbox.grid(row=6, column=0, rowspan=6, columnspan=2)
scrollbar = tk.Scrollbar(window)
scrollbar.grid(row=6, column=2, rowspan=6)
listbox.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=listbox.yview)
add_button = tk.Button(window, text='Add Patient', width=12, command=add_patient_command)
add_button.grid(row=0, column=3)
delete_button = tk.Button(window, text='Delete Patient', width=12, command=delete_patient_command)
delete_button.grid(row=1, column=3)
update_button = tk.Button(window, text='Update Patient', width=12, command=update_patient_command)
update_button.grid(row=2, column=3)
search_button = tk.Button(window, text='Search Patient', width=12, command=search_patient_command)
search_button.grid(row=3, column=3)
view_all_button = tk.Button(window, text='View All', width=12, command=view_all_command)
view_all_button.grid(row=4, column=3)
exit_button = tk.Button(window, text='Exit', width=12, command=window.destroy)
exit_button.grid(row=5, column=3)
connect_db()
window.mainloop()
```
这段代码实现了一个简单的病人管理系统,可以添加、删除、更新、查询和查看所有病人的个人信息。你可以根据自己的需求进行修改和扩展。
阅读全文