python实现简单成绩录入系统
学了一个多月的python,做了一个小程序:python实现简单成绩录入系统,实验一下
menu部分
from tkinter import*#这是一个python模块,python3中都有 import tkinter.messagebox#这也是一个模块 from file_read import read from file_write import write class student_main():#定义一个学生类 def __init__(self): self.name ='' self.ID='' self.lessons={} self.sum='' self.average='' stulist=read() stu=student_main() #登录页面-------------------------------------------------------------------- def seacharID(stulist,ID):#这是一个对是否重复id的检验 for x in stulist: if x.ID==ID: return True #这是一个成绩录入时的主要函数 def next(): root_next=Tk() root_next.title('成绩录入') root_next.geometry('200x200+230+330') var_lesson=StringVar() var_score=StringVar() Label(root_next,text='课程名').place(x=20,y=50) Label(root_next,text='成绩').place(x=20,y=90) enter_lesson=Entry(root_next,textvariable=var_lesson,width=15) enter_lesson.place(x=70,y=50) enter_score=Entry(root_next,textvariable=var_score,width=15) enter_score.place(x=70,y=90) def btn3_login(): x=var_lesson.get() y=var_score.get() if y.isdigit()==True: if int(y)>100: tkinter.messagebox.showerror('错误','请输入正确的成绩!!!') var_score.set('') elif int(y)<0: tkinter.messagebox.showerror('错误','请输入正确的成绩!!!') var_score.set('') else: stu.lessons[x]=y root_next.destroy() next() else: tkinter.messagebox.showwarning('提示','请输入数字') def btn4_login(): x=var_lesson.get() y=var_score.get() stu.lessons[x]=y tkinter.messagebox.showinfo('录入成功') root_next.destroy() btn3=Button(root_next,text='录入新学科',bg='#5EAEFF',bd=0,command=btn3_login) btn3.place(y=130,width=99,height=20) btn4=Button(root_next,text='完成',bg='#FF6868',bd=0,command=btn4_login) btn4.place(x=101,y=130,width=99,height=20) #页面循环 root_next.mainloop() #核心函数 def entry(): root_e=Tk() root_e.title("增") root_e.geometry('200x200+230+330') var_user=StringVar() var_ID=StringVar() Label(root_e,text='姓名').place(x=20,y=50) Label(root_e,text='ID').place(x=20,y=90) enter_user=Entry(root_e,textvariable=var_user,width=15) enter_user.place(x=70,y=50) enter_user.focus() enter_ID=Entry(root_e,textvariable=var_ID,width=15) enter_ID.place(x=70,y=90) def btn1_login(): x=var_user.get() y=var_ID.get() pop=seacharID(stulist,y) if pop==True: tkinter.messagebox.showwarning('错误','该学号已存在') var_ID.set("") var_user.set("") else: if y.isdigit()==True: conunt=0 sum_opp=0 stu.name=x stu.ID=y root_e.destroy() next() for x in stu.lessons.values(): sum_opp=sum_opp+int(x) conunt+=1 stu.sum=str(sum_opp) average_opp=int(stu.sum)/conunt stu.average=str(average_opp) stulist.append(stu) write(stulist) menu() else: tkinter.messagebox.showwarning('提示','您输入了非数字的内容') var_ID.set(" ") def btn2_login(): root_e.destroy() menu() btn1=Button(root_e,text='下一步',command=btn1_login,bd=0,bg='Silver') btn1.place(y=140,width=99,height=20) btn2=Button(root_e,text='取消',command=btn2_login,bd=0,bg='Silver') btn2.place(x=100,y=140,width=99,height=20) root_e.mainloop() #删除页面------------------------------------------------------------------ def deling(): root=Tk() root.title('删') root.geometry('200x200+230+330') Label(root,text='学号').place(x=40,y=50) def btn_ok(): x=var_del.get() if x.isdigit()==True: count=0 for i in stulist: if i.ID==x: stulist.remove(i) write(stulist) tkinter.messagebox.showinfo("提示",'成功删除该学生信息') root.destroy() menu() else: count+=1 if count==len(stulist): tkinter.messagebox.showinfo(title='错误',message='不存在该账户!') var_del.set('') else: tkinter.messagebox.showerror('警告','请输入数字') def btn_cancel(): x=tkinter.messagebox.askokcancel('提示','确定离开?') if x==True: root.destroy() menu() var_del=StringVar() entry_del=Entry(root,textvariable=var_del,width=10) entry_del.place(x=80,y=50) btn1=Button(root,bd=0,bg='Silver',command=btn_ok,text='确定') btn1.place(y=110,width=99,height=20) btn2=Button(root,bd=0,bg='Silver',command=btn_cancel,text='取消') btn2.place(x=101,y=110,width=99,height=20) root.mainloop() #修改页面--------------------------------------------------------------- def change(): root=Tk() root.title("改") root.geometry('200x200+230+330') Label(root,text='学号').place(x=40,y=50) var_change=StringVar() entry_change=Entry(root,textvariable=var_change,width=10) entry_change.place(x=80,y=50) def var_ok(): x=var_change.get() if x.isdigit()==True: counton=0 for i in stulist: if x==i.ID: stulist.remove(i) write(stulist) root.destroy() entry() else: counton+=1 if counton==len(stulist): tkinter.messagebox.showerror('错误','不存在该账户!') else: tkinter.messagebox.showerror('警告','输入数字ID') def var_cancel(): x=tkinter.messagebox.askokcancel('提示','确定离开') if x==True: root.destroy() btn1=Button(root,bd=0,bg='Silver',command=var_ok,text='确定') btn1.place(y=110,width=99,height=20) btn2=Button(root,bd=0,bg='Silver',command=var_cancel,text='取消') btn2.place(x=101,y=110,width=99,height=20) root.mainloop() #查询页面------------------------------------------------------------------ def next_i(x): window=Tk() window.title('显示信息') window.geometry('400x200') counton=0 for i in stulist: if i.ID==x: ai_name=i.name ai_ID=i.ID ai_lesson=str(i.lessons) ai_sum=i.sum ai_ave=i.average else: ai_name=' ' ai_ID=' ' ai_lesson='该学号不存在' ai_sum=' ' ai_ave=' ' Label(window,height=2,width=8,text=ai_name).pack() Label(window,height=2,width=8,text=ai_ID).pack() Label(window,height=2,width=100,text=ai_lesson).pack() Label(window,height=2,width=8,text=ai_sum).pack() Label(window,height=2,width=8,text=ai_ave).pack() window.mainloop() def search(): root=Tk() root.title('') root.geometry('200x200+230+330') Label(root,text='学号').place(x=40,y=50) def btn_ok(): x=var_display.get() if x.isdigit()==True: next_i(x) else: tkinter.messagebox.showerror('警告','请输入数字ID') def btn_cancel(): root.destroy() menu() var_display=StringVar() entry_display=Entry(root,textvariable=var_display,width=10) entry_display.place(x=80,y=50) btn1=Button(root,bd=0,bg='Silver',command=btn_ok,text='确定') btn1.place(y=110,width=99,height=20) btn2=Button(root,bd=0,bg='Silver',command=btn_cancel,text='返回主菜单') btn2.place(x=101,y=110,width=99,height=20) root.mainloop() #排序页面-------------------------------------------------------------- def display(x): window=Tk() window.title() contunt=0 for i in x: contunt+=1 all_list=['第'+str(contunt)+'名:'] all_list.append('姓名:'+i.name+'\\') all_list.append('学号:'+i.ID+'\\') for m,n in i.lessons.items(): all_list.append(m+":") all_list.append(n) all_list.append('\\') all_list.append('总分:'+i.sum+'\\') all_list.append('平均分:'+i.average) Label(window,bd=20,text=all_list).pack() window.mainloop() def sorting(): for i in range(len(stulist)-1): for j in range(i+1,len(stulist)): temp=student_main() if stulist[i].average<stulist[j].average: temp=stulist[i] stulist[i]=stulist[j] stulist[j]=temp display(stulist) #菜单------------------------------------------------------ def menu(): root=Tk() root.title('主视面') root.geometry('250x300+150+100') def var_one(): root.destroy() entry() def var_two(): root.destroy() deling() def var_three(): root.destroy() change() def var_four(): root.destroy() search() def var_five(): sorting() def var_six(): root.destroy() var0=Label(root,text='--------------菜单--------------',font=('Arial',13),width=30,height=1) var0.pack() var1=Button(root,text='1.添加界面',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_one) var1.pack(pady=4) var2=Button(root,text='2.删除界面',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_two) var2.pack(pady=4) var3=Button(root,text='3.更改界面',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_three) var3.pack(pady=4) var4=Button(root,text='4.查询界面',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_four) var4.pack(pady=4) var4=Button(root,text='5.成绩排序',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_five) var4.pack(pady=4) var6=Button(root,text='退出',bg='Silver',font=('Arial',12),width=20,height=1,bd=0,command=var_six) var6.pack(pady=4) root.mainloop() menu()
file_read模块部分
(file_read主要是将我储存在txt文件中的数据转化成stulist列表,来进行检验,防止录入同一个id)
class student_main(): def __init__(self): self.name ='' self.ID='' self.lessons={} self.sum='' self.average='' def read(): stulist=[] x=[] f=open('all_student.txt','r') t=open('all_lesson.txt','r') line=f.readlines() lene=t.readlines() for i in range(len(line)): if (i+1)%2==1:#这里主要是读取和储存是出现了莫名的空行,我只好多录入一个空行好方便读取 y=student_main() x=line[i].split(" ") y.name=x[0] y.ID=x[1] j=int((i+1)/2) z=eval(lene[j]) y.lessons=z y.sum=x[2] y.average=x[3] stulist.append(y) return stulist
file_write模块部分
(主要是将menu中获得的数据进行储存)
class student_main(): def __init__(self): self.name ='' self.ID='' self.lessons={} self.sum='' self.average='' def write(stulist): f=open('all_student.txt','w') for i in stulist: f.write(i.name+' '+i.ID+' '+i.sum+' '+i.average) f.write('\n') f.close() t=open('all_lesson.txt','w') for i in stulist: t.write(str(i.lessons)) t.write('\n') t.close()
还要自己建立两个TXT文本,一个叫all_lesson.txt,另一个叫all_student.txt。
因为student类中的lesson是用字典储存的,我实在找不到怎么把他完整的读出来的语句只好分开存储了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)