import pymssql
import tkinter as tk
import tkinter.messagebox
from tkinter import ttk
def dbTools():
conn = pymssql.connect('DESKTOP-9NADTHD', 'sa', 'shiyike', 'hospital')
cursor = conn.cursor()
return conn, cursor
class Login(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("病人信息管理系统")
self.root.geometry('500x300')
self.label_account = tkinter.Label(self.root, text='账户: ')
self.label_password = tkinter.Label(self.root, text='密码: ')
self.input_account = tkinter.Entry(self.root, width=30)
self.input_password = tkinter.Entry(self.root, show='*', width=30)
self.login_button = tkinter.Button(self.root,
command=self.backstage_interface,
text="登录",
width=10)
self._gui_arrang()
def _gui_arrang(self):
self.label_account.place(x=60, y=50)
self.label_password.place(x=60, y=75)
self.input_account.place(x=110, y=50)
self.input_password.place(x=110, y=75)
self.login_button.place(x=150, y=105)
def backstage_interface(self):
account = self.input_account.get()
password = self.input_password.get()
if (len(account) == 0 or len(password) == 0):
tkinter.messagebox.showerror(message='用户名或密码为空')
return
conn, cursor = dbTools()
sql = "select * from docter where id={0} and pwd='{1}'".format(
account, password)
cursor.execute(sql)
rows=cursor.fetchone()
cursor.close()
conn.close()
if rows!=None:
tkinter.messagebox.showinfo(title='登录成功', message='登录成功')
self.root.destroy()
o = Menu(account)
else:
tkinter.messagebox.showinfo(title='登录失败', message='登录失败')
class Menu(object):
def __init__(self, id):
self.root = tkinter.Tk()
self.root.title("医院管理系统")
self.root.geometry('230x220')
self.id = id
self.record_patient_button = tkinter.Button(
self.root, text="病人登记", width=10, command=self._intoRecordPatient)
self.query_patient_button = tkinter.Button(
self.root, text="病人查询", width=10, command=self._intoQueryPatient)
self.query_pa_list_button = tkinter.Button(
self.root, text="病例查询", width=10, command=self._intoQueryPaList)
self.add_pa_list_button = tkinter.Button(self.root,
text="新增病例",
width=10,
command=self._intoAddPaList)
self.admin_button = tkinter.Button(self.root,
text="管理中心",
width=10,
command=self._intoAdmin)
self.label_information = tkinter.Label(self.root, text='')
self._infomation_get()
self._gui_arrang()
def _intoRecordPatient(self):
i = RecordPatient()
def _intoQueryPatient(self):
q = QueryPatient()
def _intoQueryPaList(self):
q = QueryPaList()
def _intoAddPaList(self):
i = AddPaList(self.id)
def _intoAdmin(self):
if self.admin:
a = Admin()
else:
tkinter.messagebox.showerror('没有权限')
def _infomation_get(self):
conn, cursor = dbTools()
sql = "select docter.id,docter.name,type.name,type.isadmin from docter,type where docter.id={0} and docter.belong=type.id".format(
self.id)
cursor.execute(sql)
result = cursor.fetchone()
self.label_information['text'] = '欢迎您,{0}:{1}{2}'.format(
result[2], result[0], result[1])
self.admin = result[3]
cursor.close()
conn.close()
def _gui_arrang(self):
self.record_patient_button.place(x=20, y=40)
self.query_patient_button.place(x=120, y=40)
self.query_pa_list_button.place(x=20, y=100)
self.add_pa_list_button.place(x=120, y=100)
self.admin_button.place(x=60, y=160)
self.label_information.place(x=50, y=10)
class RecordPatient(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("病人登记")
self.root.geometry('300x250')
self.cmb = ttk.Combobox(self.root, width=10)
self.cmb['values'] = ('男', '女')
self.cmb.current(0)
self.name_label = tkinter.Label(self.root, text='姓名: ')
self.birthdate_label = tkinter.Label(self.root,
text='出生日期: \n(yyyy-mm-dd)')
self.sex_label = tkinter.Label(self.root, text='性别: ')
self.input_name = tkinter.Entry(self.root, width=12)
self.input_birthdate = tkinter.Entry(self.root, width=12)
self.okk_button = tkinter.Button(self.root,
text="登记",
width=10,
command=self._confirm)
self._gui_arrang()
def _gui_arrang(self):
self.name_label.place(x=20, y=20)
self.birthdate_label.place(x=20, y=80)
self.sex_label.place(x=20, y=140)
self.input_name.place(x=160, y=20)
self.input_birthdate.place(x=160, y=80)
self.cmb.place(x=160, y=140)
self.okk_button.place(x=90, y=180)
def _confirm(self):
name = self.input_name.get()
birthdate = self.input_birthdate.get()
sex = self.cmb.get()
if not (len(name) and len(birthdate) and len(sex)):
tkinter.messagebox.showerror(message='有内容为空')
return
conn, cursor = dbTools()
sql = "INSERT INTO patient (name, birthday, sex) VALUES ('{0}', '{1}', '{2}')".format(
name, birthdate, sex)
cursor.execute(sql)
conn.commit()
sql = 'select max(id) from patient'
cursor.execute(sql)
result = cursor.fetchone()[0]
conn.close()
tkinter.messagebox.showinfo('注册成功', '注册成功!,病人id为{0}'.format(result))
self.input_name.delete(0, 'end')
self.input_birthdate.delete(0, 'end')
class QueryPatient(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("病人查询")
self.root.geometry('300x250')
self.id_label = tkinter.Label(self.root, text='账号: ')
self.input_id = tkinter.Entry(self.root, width=12)
self.okk_button = tkinter.Button(self.root,
text="查询",
width=10,
command=self._query)
mat = "{:^20}\t{:^20}\t{:^20}"
self.title = tkinter.Label(self.root,
text=mat.format('姓名', '生日', '性别'))
self.content = tkinter.Label(self.root, text='')
self._gui_arrang()
def _gui_arrang(self):
self.id_label.place(x=20, y=20)
self.input_id.place(x=160, y=20)
self.okk_button.place(x=90, y=60)
self.title.place(x=0, y=100)
self.content.place(x=0, y=130)
def _query(self):
id = self.input_id.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select name, birthday, sex from patient where id={0} ".format(
id)
cursor.execute(sql)
result = cursor.fetchone()
conn.close()
if not result:
tkinter.messagebox.showerror(message='未查询到记录')
self.content['text'] = ''
return
mat = "{:^20}\t{:^20}\t{:^20}"
self.content['text'] = mat.format(str(result[0]), str(result[1]),
str(result[2]))
class QueryPaList(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("病例查询")
self.root.geometry('300x250')
self.id_label = tkinter.Label(self.root, text='账号: ')
self.input_id = tkinter.Entry(self.root, width=12)
self.okk_button = tkinter.Button(self.root,
text="查询",
width=10,
command=self._query)
self._gui_arrang()
def _gui_arrang(self):
self.id_label.place(x=20, y=20)
self.input_id.place(x=160, y=20)
self.okk_button.place(x=90, y=60)
def _query(self):
id = self.input_id.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select name, birthday, sex from patient where id={0} ".format(
id)
cursor.execute(sql)
result = cursor.fetchone()
conn.close()
if not result:
tkinter.messagebox.showerror(message='未查询到记录')
else:
GetPalist(id)
class GetPalist(object):
def __init__(self, id):
self.root = tkinter.Tk()
self.root.title("id{0}病例".format(id))
self.id = id
self.scrollbar = tkinter.Scrollbar(self.root)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
title = [
'1',
'2',
'3',
'4',
'5',
]
self.box = ttk.Treeview(self.root,
columns=title,
yscrollcommand=self.scrollbar.set,
show='headings')
self.box.column('1', width=80, anchor='center')
self.box.column('2', width=200, anchor='center')
self.box.column('3', width=300, anchor='center')
self.box.column('4', width=80, anchor='center')
self.box.column('5', width=80, anchor='center')
self.box.heading('1', text='姓名')
self.box.heading('2', text='日期')
self.box.heading('3', text='病例')
self.box.heading('4', text='医生')
self.box.heading('5', text='部门')
self._get()
self.scrollbar.config(command=self.box.yview)
self.box.pack()
def _get(self):
conn, cursor = dbTools()
sql = "select paName,date,records,docName,dcoType from palistview where paID={0}".format(
self.id)
cursor.execute(sql)
results = cursor.fetchall()
if results == None:
tkinter.messagebox.showerror(message='未查询到记录')
self.root.destroy()
for row in results:
self.box.insert('', 'end', values=row)
conn.close()
cursor.close()
class AddPaList(object):
def __init__(self, id):
self.id = id
self.root = tkinter.Tk()
self.root.title("病例查询")
self.root.geometry('300x250')
self.id_label = tkinter.Label(self.root, text='病人账号: ')
self.input_id = tkinter.Entry(self.root, width=12)
self.okk_button = tkinter.Button(self.root,
text="提交",
width=10,
command=self._query)
self.t = tk.Text(self.root, height=10, width=40)
self._gui_arrang()
def _gui_arrang(self):
self.id_label.place(x=20, y=20)
self.input_id.place(x=160, y=20)
self.okk_button.place(x=90, y=50)
self.t.place(x=0, y=100)
def _query(self):
id = self.input_id.get()
info = self.t.get("0.0", "end")
if not (len(id) and len(info)):
return
conn, cursor = dbTools()
sql = "select * from patient where id={0} ".format(id)
cursor.execute(sql)
result = cursor.fetchone()
if not result:
tkinter.messagebox.showerror(message='未查询到此病人')
self.content['text'] = ''
return
sql = "INSERT INTO pa_list (patient, docter, info) VALUES ('{0}', '{1}', '{2}')".format(
id, self.id, info)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '插入成功')
class Admin(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("管理中心")
self.root.geometry('230x220')
self.id = id
self.doc_button = tkinter.Button(self.root,
text="医生管理",
width=10,
command=self._intoDocterQuery)
self.med_button = tkinter.Button(self.root,
text="药品管理",
width=10,
command=self._intoMedicine)
self.all_doc = tkinter.Button(self.root,
text="医生一览",
width=10,
command=self._intoalldoc)
self.all_med = tkinter.Button(self.root,
text="药品一览",
width=10,
command=self._intoallmed)
self._gui_arrang()
def _intoDocterQuery(self):
d = docterAdmin()
def _intoMedicine(self):
m = medicineAdmin()
def _intoalldoc(self):
a = AllDocter()
def _intoallmed(self):
a = AllMedicine()
def _gui_arrang(self):
self.doc_button.place(x=20, y=40)
self.med_button.place(x=120, y=40)
self.all_doc.place(x=20, y=100)
self.all_med.place(x=120, y=100)
class docterAdmin(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("医生管理")
self.root.geometry('500x300')
self.account = tkinter.Label(self.root, text='id')
self.name = tkinter.Label(self.root, text='姓名')
self.birthday = tkinter.Label(self.root, text='生日')
self.pwd = tkinter.Label(self.root, text='密码')
self.belong = tkinter.Label(self.root, text='所属部门id')
self.sex = tkinter.Label(self.root, text='性别')
self.input_account = tkinter.Entry(self.root, width=10)
self.input_name = tkinter.Entry(self.root, width=10)
self.input_birthday = tkinter.Entry(self.root, width=10)
self.input_pwd = tkinter.Entry(self.root, width=10)
self.input_belong = tkinter.Entry(self.root, width=10)
self.input_sex = tkinter.Entry(self.root, width=10)
self.query = tkinter.Button(self.root,
text="查询",
width=10,
command=self._Query)
self.update = tkinter.Button(self.root,
text="更新",
width=10,
command=self._Update)
self.insert = tkinter.Button(self.root,
text="插入",
width=10,
command=self._Insert)
self.delete = tkinter.Button(self.root,
text="删除",
width=10,
command=self._Delete)
self._gui_arrang()
def _gui_arrang(self):
self.account.place(x=20, y=0)
self.name.place(x=200, y=0)
self.birthday.place(x=380, y=0)
self.pwd.place(x=20, y=100)
self.belong.place(x=200, y=100)
self.sex.place(x=380, y=100)
self.input_account.place(x=20, y=50)
self.input_name.place(x=200, y=50)
self.input_birthday.place(x=380, y=50)
self.input_pwd.place(x=20, y=150)
self.input_belong.place(x=200, y=150)
self.input_sex.place(x=380, y=150)
self.query.place(x=40, y=200)
self.update.place(x=140, y=200)
self.insert.place(x=240, y=200)
self.delete.place(x=340, y=200)
def _Query(self):
id = self.input_account.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select id,name,birthdate,pwd,belong,sex from docter where id={0}".format(
id)
cursor.execute(sql)
result = cursor.fetchone()
conn.close()
if result == None:
tkinter.messagebox.showerror('无此记录', '无此记录')
return
v = tkinter.StringVar(self.root)
v.set(result[1])
self.input_name['textvariable'] = v
v = tkinter.StringVar(self.root)
v.set(result[2])
self.input_birthday['textvariable'] = v
v = tkinter.StringVar(self.root)
v.set(result[3])
self.input_pwd['textvariable'] = v
v = tkinter.StringVar(self.root)
v.set(result[4])
self.input_belong['textvariable'] = v
v = tkinter.StringVar(self.root)
v.set(result[5])
self.input_sex['textvariable'] = v
def _Delete(self):
id = self.input_account.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select * from docter where id={0}".format(id)
cursor.execute(sql)
result = cursor.fetchone()
if result == None:
conn.close()
tkinter.messagebox.showerror('无此记录', '无此记录')
return
sql = "delete from docter where id={0}".format(id)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '删除成功')
self.input_account.delete(0, 'end')
def _Insert(self):
name = self.input_name.get()
birthday = self.input_birthday.get()
pwd = self.input_pwd.get()
belong = self.input_belong.get()
sex = self.input_sex.get()
if not (len(name) and len(birthday) and len(pwd) and len(belong)
and len(sex)):
tkinter.messagebox.showerror('内容为空', '内容为空')
conn, cursor = dbTools()
sql = "INSERT INTO docter (name, birthdate, pwd, belong, sex) VALUES ('{0}','{1}', '{2}',{3}, '{4}')".format(
name, birthday, pwd, belong, sex)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '插入成功')
def _Update(self):
id = self.input_account.get()
name = self.input_name.get()
birthday = self.input_birthday.get()
pwd = self.input_pwd.get()
belong = self.input_belong.get()
sex = self.input_sex.get()
if not (len(name) and len(birthday) and len(pwd) and len(belong)
and len(sex) and len(id)):
tkinter.messagebox.showerror('内容为空', '内容为空')
conn, cursor = dbTools()
sql = "select * from docter where id={0}".format(id)
cursor.execute(sql)
result = cursor.fetchone()
if result == None:
conn.close()
tkinter.messagebox.showerror('无此记录', '无此记录')
return
sql = '''update docter set name='{0}',birthdate='{1}', pwd='{2}' ,belong={3},sex='{4}' where id={5}'''.format(
name, birthday, pwd, belong, sex, id)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '更新成功')
class AllMedicine(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("库存和商品一览")
self.scrollbar = tkinter.Scrollbar(self.root)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
title = [
'1',
'2',
'3',
]
self.box = ttk.Treeview(self.root,
columns=title,
yscrollcommand=self.scrollbar.set,
show='headings')
self.box.column('1', width=100, anchor='center')
self.box.column('2', width=100, anchor='center')
self.box.column('3', width=100, anchor='center')
self.box.heading('1', text='编号')
self.box.heading('2', text='药名')
self.box.heading('3', text='售价')
self._get()
self.scrollbar.config(command=self.box.yview)
self.box.pack()
def _get(self):
conn, cursor = dbTools()
sql = "select * from medicine "
cursor.execute(sql)
results = cursor.fetchall()
if results == None:
return
for row in results:
self.box.insert('', 'end', values=row)
conn.close()
cursor.close()
class AllDocter(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("库存和商品一览")
self.scrollbar = tkinter.Scrollbar(self.root)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
title = ['1', '2', '3', '4', '5', '6']
self.box = ttk.Treeview(self.root,
columns=title,
yscrollcommand=self.scrollbar.set,
show='headings')
self.box.column('1', width=100, anchor='center')
self.box.column('2', width=100, anchor='center')
self.box.column('3', width=100, anchor='center')
self.box.column('4', width=100, anchor='center')
self.box.column('5', width=100, anchor='center')
self.box.column('6', width=100, anchor='center')
self.box.heading('1', text='编号')
self.box.heading('2', text='姓名')
self.box.heading('3', text='生日')
self.box.heading('4', text='密码')
self.box.heading('5', text='部门')
self.box.heading('6', text='性别')
self._get()
self.scrollbar.config(command=self.box.yview)
self.box.pack()
def _get(self):
conn, cursor = dbTools()
sql = "select * from docter "
cursor.execute(sql)
results = cursor.fetchall()
if results == None:
return
for row in results:
self.box.insert('', 'end', values=row)
conn.close()
cursor.close()
class medicineAdmin(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title("药品管理")
self.root.geometry('500x200')
self.id = tkinter.Label(self.root, text='id')
self.name = tkinter.Label(self.root, text='名称')
self.price = tkinter.Label(self.root, text='价格')
self.input_id = tkinter.Entry(self.root, width=10)
self.input_name = tkinter.Entry(self.root, width=10)
self.input_price = tkinter.Entry(self.root, width=10)
self.query = tkinter.Button(self.root,
text="查询",
width=10,
command=self._Query)
self.update = tkinter.Button(self.root,
text="更新",
width=10,
command=self._Update)
self.insert = tkinter.Button(self.root,
text="插入",
width=10,
command=self._Insert)
self.delete = tkinter.Button(self.root,
text="删除",
width=10,
command=self._Delete)
self._gui_arrang()
def _gui_arrang(self):
self.id.place(x=20, y=0)
self.name.place(x=200, y=0)
self.price.place(x=380, y=0)
self.input_id.place(x=20, y=50)
self.input_name.place(x=200, y=50)
self.input_price.place(x=380, y=50)
self.query.place(x=40, y=100)
self.update.place(x=140, y=100)
self.insert.place(x=240, y=100)
self.delete.place(x=340, y=100)
def _Query(self):
id = self.input_id.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select id,name,price from medicine where id={0}".format(id)
cursor.execute(sql)
result = cursor.fetchone()
conn.close()
if result == None:
tkinter.messagebox.showerror('无此记录', '无此记录')
return
v=tkinter.StringVar(self.root)
v.set(result[1])
self.input_name['textvariable'] = v
v=tkinter.StringVar(self.root)
v.set(result[2])
self.input_price['textvariable'] =v
def _Delete(self):
id = self.input_id.get()
if not len(id):
return
conn, cursor = dbTools()
sql = "select * from medicine where id={0}".format(id)
cursor.execute(sql)
result = cursor.fetchone()
if result == None:
conn.close()
tkinter.messagebox.showerror('无此记录', '无此记录')
return
sql = "delete from medicine where id={0}".format(id)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '删除成功')
self.input_id.delete(0, 'end')
def _Insert(self):
name = self.input_name.get()
price = self.input_price.get()
if not (len(name) and len(price)):
tkinter.messagebox.showerror('内容为空', '内容为空')
conn, cursor = dbTools()
sql = "INSERT INTO medicine (name, price) VALUES ('{0}',{1})".format(
name, price)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '插入成功')
def _Update(self):
id = self.input_id.get()
name = self.input_name.get()
price = self.input_price.get()
if not (len(name) and len(price) and len(id)):
tkinter.messagebox.showerror('内容为空', '内容为空')
conn, cursor = dbTools()
sql = "select * from medicine where id={0}".format(id)
cursor.execute(sql)
result = cursor.fetchone()
if result == None:
conn.close()
tkinter.messagebox.showerror('无此记录', '无此记录')
return
sql = '''update medicine set name='{0}',price='{1}' where id={2}'''.format(
name, price, id)
cursor.execute(sql)
conn.commit()
conn.close()
tkinter.messagebox.showinfo('成功', '更新成功')
if __name__ == '__main__':
L = Login()
tkinter.mainloop()