You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
9.1 KiB

# -*- coding: utf-8 -*-
import tkinter as tk
LIST = []
def writeTxt_wMode(studentList):
with open("test.txt", "w", encoding="utf-8") as f:
for i in range(0, len(studentList)):
DICT = studentList[i]
if i == len(studentList) - 1:
f.write("{0}\t{1}\t{2}".format(DICT["name"], DICT["sex"], DICT["phone"]))
else:
f.write("{0}\t{1}\t{2}\n".format(DICT["name"], DICT["sex"], DICT["phone"]))
def readTxt() -> list:
emptyList1 = []
emptyList2 = []
f = open("./test.txt", 'r', encoding='UTF-8')
for i in f:
a = str(i)
b = a.replace('\n', '')
emptyList1.append(b.split("\t"))
while len(emptyList2) < len(emptyList1):
for j in range(0, len(emptyList1)):
name = emptyList1[j][0]
sex = emptyList1[j][1]
phone = emptyList1[j][2]
Dict = {"name": name, "sex": sex, "phone": phone}
emptyList2.append(Dict)
f.close()
return emptyList2
def checkPhoneNumber_Boolean(phone):
if phone.isdigit():
return True
else:
result.set("电话号码输入不是纯数字")
return False
def trim(strings):
while strings[:1] == ' ':
strings = strings[1:]
while strings[-1:] == ' ':
strings = strings[:-1]
return strings
def checkTheSameName(enterName):
flagList = [] # 存的是名字的索引
List = []
returnList = []
date = 1
for i in range(0, len(LIST)):
if LIST[i]["name"] == enterName:
flagList.append(int(i))
if len(flagList) == 1:
return date # 返回1表示不重名
if len(flagList) > 1:
for i in flagList:
List.append(LIST[i])
returnList.append(List)
returnList.append(flagList)
return returnList # 第一个元素是列表,第二个元素是存储重复名字的索引的 列表
# 定义方法显示所有重复的学生信息
def show_ofTheSameName(list_ofTheSameName):
str_out = ""
str_out += "出现同名,同名学生信息显示如下:\n"
str_out += ("{:^9}".format("学生序号") +
"{:^9}".format("学生姓名") +
"{:^9}".format("学生性别") +
"{:^11}".format("电话号码") +
"\n")
for i in range(0, len(list_ofTheSameName)):
str_out += ("{:<11}".format(i + 1, chr(12288)) +
"{:<11}".format(list_ofTheSameName[i].get("name"), chr(12288)) +
"{:^9}".format(list_ofTheSameName[i].get("sex"), chr(12288)) +
"{:>15}".format(list_ofTheSameName[i].get("phone"), chr(12288)) +
"\n")
str_out += "\n请在上方输入详细学生信息,进行精准删除"
result.set(str_out)
def delete_Gui_StudentMessage():
textName.set("")
textSex.set("")
textPhone.set("")
def addStudentsMessage():
name = textName.get()
if not name:
result.set("学生姓名不能为空")
return
if trim(name) == "":
result.set("学生姓名不能为空")
return
sex = textSex.get()
if sex == "" or sex == "":
pass
else:
result.set("性别输入格式有误")
return
phone = textPhone.get()
resultBoolean = checkPhoneNumber_Boolean(phone)
if resultBoolean:
pass
else:
return
Dict = {"name": name, "sex": sex, "phone": phone}
LIST.append(Dict)
result.set("学生已添加")
delete_Gui_StudentMessage()
writeTxt_wMode(LIST)
return
# 2、删除学生信息
def deleteMessage() -> list:
# 定义信号位
flag = True
enterName = textName.get()
for i in range(0, len(LIST)):
if LIST[i]["name"] == enterName:
flag = False
break
if flag:
result.set("学生不存在")
return LIST
# 开始遍历查找
accept = checkTheSameName(enterName) # 接收查找同名方法的返回值
if accept == 1:
for i in range(0, len(LIST)):
if LIST[i]["name"] == enterName:
del LIST[i]
result.set("删除成功")
writeTxt_wMode(LIST)
delete_Gui_StudentMessage()
# 删除成功直接返回,结束该方法
return
else:
show_ofTheSameName(accept[0])
for i in range(0, len(LIST)):
if LIST[i]["name"] == textName.get():
if LIST[i]["sex"] == textSex.get():
if LIST[i]["phone"] == textPhone.get():
del LIST[i]
result.set("删除成功")
writeTxt_wMode(LIST)
delete_Gui_StudentMessage()
return
# 3、修改学生信息
def change():
if LIST is None:
result.set("没有信息,无法修改")
return None
if len(LIST) == 0:
result.set("没有信息,无法修改")
return None
if len(LIST) != 0:
inputName = textName.get()
for i in range(0, len(LIST)):
if LIST[i]["name"] == inputName:
LIST[i]["sex"] = textSex.get()
LIST[i]["phone"] = textSex.get()
writeTxt_wMode(LIST)
result.set("修改成功")
delete_Gui_StudentMessage()
return
# 4、显示所有学生信息
def show():
str_out = ""
if LIST is None:
result.set("无学生信息")
return
if len(LIST) == 0:
result.set("无学生信息")
return
if len(LIST) != 0:
str_out += "学生信息如下:\n"
str_out += ("{:^9}".format("学生序号") +
"{:^9}".format("学生姓名") +
"{:^9}".format("学生性别") +
"{:^11}".format("电话号码") +
"\n")
for i in range(0, len(LIST)):
str_out += ("{:<11}".format(i + 1, chr(12288)) +
"{:<11}".format(LIST[i].get("name"), chr(12288)) +
"{:^9}".format(LIST[i].get("sex"), chr(12288)) +
"{:>15}".format(LIST[i].get("phone"), chr(12288)) +
"\n")
result.set(str_out)
try:
for i in readTxt():
LIST.append(i)
except:
pass
if __name__ == '__main__':
# 以下代码全部为gui界面的初始化
window = tk.Tk()
window.title('学生信息管理系统 ')
window.geometry('500x650')
result = tk.StringVar()
result.set(" ")
topLine_school = tk.Label(window, text="湖南工业大学科技学院", font=('宋体', 13), width=20).place(x=150, y=10,anchor='nw')
textName = tk.StringVar()
textName.set("")
labelLine1 = tk.Label(window, text="姓 名:", font=('Arial', 15), width=10).place(x=75, y=50, anchor='nw')
entryLine1 = tk.Entry(window, show=None, font=('宋体', 15), textvariable=textName, width=20)
entryLine1.place(x=190, y=50, anchor='nw') # 显示成明文形式
textSex = tk.StringVar()
textSex.set("")
labelLine2 = tk.Label(window, text="性 别:", font=('Arial', 15), width=10)
labelLine2.place(x=75, y=100, anchor='nw')
entryLine2 = tk.Entry(window, show=None, font=('Arial', 15), textvariable=textSex, width=18)
entryLine2.place(x=190, y=100, anchor='nw')
textPhone = tk.StringVar()
textPhone.set("")
labelLine3 = tk.Label(window, text="电 话:", font=('Arial', 15), width=10).place(x=75, y=150, anchor='nw')
entryLine3 = tk.Entry(window, show=None, font=('Arial', 15), textvariable=textPhone, width=18)
entryLine3.place(x=190, y=150, anchor='nw')
button1_add = tk.Button(window, text='添 加', bg='silver', font=('Arial', 12), command=addStudentsMessage,
width=8)
button1_add.place(x=40, y=220, anchor='nw')
button2_delete = tk.Button(window, text='删 除', bg='silver', font=('Arial', 12), command=deleteMessage, width=8)
button2_delete.place(x=150, y=220, anchor='nw')
button3_change = tk.Button(window, text='修 改', bg='silver', font=('Arial', 12), command=change, width=8)
button3_change.place(x=260, y=220, anchor='nw')
button4_show = tk.Button(window, text='显 示', bg='silver', font=('Arial', 12), command=show, width=8)
button4_show.place(x=370, y=220, anchor='nw')
labelLine_Name = tk.Label(window, text="@Author:", font=('宋体', 13), width=10).place(x=130, y=260, anchor='nw')
labelLine_myID = tk.Label(window, text="陈浩", font=('宋体', 13), width=10).place(x=250, y=260, anchor='nw')
show_result = tk.Label(window, bg="white", fg="black", font=("宋体", 12), bd='0', anchor='nw', textvariable=result)
show_result.place(x="25", y="300", width="450", height="300")
window.mainloop()