python用户交互图形界面患者信息多次保存查询删除界面的代码
时间: 2023-09-01 20:13:29 浏览: 122
Python图形界面
以下是一个简单的Python用户交互图形界面(GUI)的例子,用于保存、查询和删除患者信息:
```python
import tkinter as tk
class Patient:
def __init__(self, name, age, gender, address):
self.name = name
self.age = age
self.gender = gender
self.address = address
class PatientDatabase:
def __init__(self):
self.patients = []
def add_patient(self, patient):
self.patients.append(patient)
def find_patient(self, name):
for patient in self.patients:
if patient.name == name:
return patient
return None
def remove_patient(self, name):
patient = self.find_patient(name)
if patient:
self.patients.remove(patient)
return True
else:
return False
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("Patient Database")
self.patient_db = PatientDatabase()
self.create_widgets()
def create_widgets(self):
# Labels
self.name_label = tk.Label(self.master, text="Name:")
self.name_label.grid(row=0, column=0)
self.age_label = tk.Label(self.master, text="Age:")
self.age_label.grid(row=1, column=0)
self.gender_label = tk.Label(self.master, text="Gender:")
self.gender_label.grid(row=2, column=0)
self.address_label = tk.Label(self.master, text="Address:")
self.address_label.grid(row=3, column=0)
# Entry fields
self.name_entry = tk.Entry(self.master)
self.name_entry.grid(row=0, column=1)
self.age_entry = tk.Entry(self.master)
self.age_entry.grid(row=1, column=1)
self.gender_entry = tk.Entry(self.master)
self.gender_entry.grid(row=2, column=1)
self.address_entry = tk.Entry(self.master)
self.address_entry.grid(row=3, column=1)
# Buttons
self.save_button = tk.Button(self.master, text="Save", command=self.save_patient)
self.save_button.grid(row=4, column=0)
self.find_button = tk.Button(self.master, text="Find", command=self.find_patient)
self.find_button.grid(row=4, column=1)
self.remove_button = tk.Button(self.master, text="Remove", command=self.remove_patient)
self.remove_button.grid(row=4, column=2)
# Text box
self.text_box = tk.Text(self.master, height=10, width=50)
self.text_box.grid(row=5, columnspan=3)
def save_patient(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
address = self.address_entry.get()
patient = Patient(name, age, gender, address)
self.patient_db.add_patient(patient)
self.text_box.insert(tk.END, f"Patient {name} added to database.\n")
def find_patient(self):
name = self.name_entry.get()
patient = self.patient_db.find_patient(name)
if patient:
self.text_box.insert(tk.END, f"Patient found:\nName: {patient.name}\nAge: {patient.age}\nGender: {patient.gender}\nAddress: {patient.address}\n")
else:
self.text_box.insert(tk.END, f"Patient {name} not found in database.\n")
def remove_patient(self):
name = self.name_entry.get()
if self.patient_db.remove_patient(name):
self.text_box.insert(tk.END, f"Patient {name} removed from database.\n")
else:
self.text_box.insert(tk.END, f"Patient {name} not found in database.\n")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
在上面的代码中,我们定义了一个`Patient`类来表示患者,一个`PatientDatabase`类来表示患者数据库,并且使用`tkinter`库来创建GUI。
在`Application`类的构造函数中,我们创建了`PatientDatabase`对象,并且调用了`create_widgets()`函数来创建GUI的各个部分。在`create_widgets()`函数中,我们创建了标签、输入框、按钮和文本框。
当用户点击“Save”按钮时,我们从输入框中获取患者的信息,并创建一个`Patient`对象,然后将其添加到`PatientDatabase`对象中。在文本框中显示一条消息,告诉用户患者已经被添加到数据库中。
当用户点击“Find”按钮时,我们从输入框中获取患者的名字,并在`PatientDatabase`对象中查找该患者。如果患者存在,则在文本框中显示患者的信息。否则,在文本框中显示一条消息,告诉用户该患者不存在于数据库中。
当用户点击“Remove”按钮时,我们从输入框中获取患者的名字,并在`PatientDatabase`对象中删除该患者。如果患者存在,则在文本框中显示一条消息,告诉用户该患者已从数据库中删除。否则,在文本框中显示一条消息,告诉用户该患者不存在于数据库中。
阅读全文