您好,登錄后才能下訂單哦!
學了一個多月的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是用字典儲存的,我實在找不到怎么把他完整的讀出來的語句只好分開存儲了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。