python實(shí)現(xiàn)簡單成績錄入系統(tǒng)
學(xué)了一個(gè)多月的python,做了一個(gè)小程序:python實(shí)現(xiàn)簡單成績錄入系統(tǒng),實(shí)驗(yàn)一下
menu部分
from tkinter import*#這是一個(gè)python模塊,python3中都有
import tkinter.messagebox#這也是一個(gè)模塊
from file_read import read
from file_write import write
class student_main():#定義一個(gè)學(xué)生類
def __init__(self):
self.name =''
self.ID=''
self.lessons={}
self.sum=''
self.average=''
stulist=read()
stu=student_main()
#登錄頁面--------------------------------------------------------------------
def seacharID(stulist,ID):#這是一個(gè)對是否重復(fù)id的檢驗(yàn)
for x in stulist:
if x.ID==ID:
return True
#這是一個(gè)成績錄入時(shí)的主要函數(shù)
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('錯(cuò)誤','請輸入正確的成績?。?!')
var_score.set('')
elif int(y)<0:
tkinter.messagebox.showerror('錯(cuò)誤','請輸入正確的成績?。?!')
var_score.set('')
else:
stu.lessons[x]=y
root_next.destroy()
next()
else:
tkinter.messagebox.showwarning('提示','請輸入數(shù)字')
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='錄入新學(xué)科',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)
#頁面循環(huán)
root_next.mainloop()
#核心函數(shù)
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('錯(cuò)誤','該學(xué)號已存在')
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('提示','您輸入了非數(shù)字的內(nèi)容')
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='學(xué)號').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("提示",'成功刪除該學(xué)生信息')
root.destroy()
menu()
else:
count+=1
if count==len(stulist):
tkinter.messagebox.showinfo(title='錯(cuò)誤',message='不存在該賬戶!')
var_del.set('')
else:
tkinter.messagebox.showerror('警告','請輸入數(shù)字')
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='學(xué)號').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('錯(cuò)誤','不存在該賬戶!')
else:
tkinter.messagebox.showerror('警告','輸入數(shù)字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='該學(xué)號不存在'
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='學(xué)號').place(x=40,y=50)
def btn_ok():
x=var_display.get()
if x.isdigit()==True:
next_i(x)
else:
tkinter.messagebox.showerror('警告','請輸入數(shù)字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('學(xué)號:'+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文件中的數(shù)據(jù)轉(zhuǎn)化成stulist列表,來進(jìn)行檢驗(yàn),防止錄入同一個(gè)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:#這里主要是讀取和儲存是出現(xiàn)了莫名的空行,我只好多錄入一個(gè)空行好方便讀取
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中獲得的數(shù)據(jù)進(jìn)行儲存)
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()
還要自己建立兩個(gè)TXT文本,一個(gè)叫all_lesson.txt,另一個(gè)叫all_student.txt。
因?yàn)閟tudent類中的lesson是用字典儲存的,我實(shí)在找不到怎么把他完整的讀出來的語句只好分開存儲了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實(shí)現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子
這篇文章主要介紹了Python 實(shí)現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python flashtext文本搜索和替換操作庫功能使用探索
本文將深入介紹Python flashtext庫,包括其基本用法、功能特性、示例代碼以及實(shí)際應(yīng)用場景,以幫助大家更好地利用這個(gè)有用的工具2024-01-01
Python實(shí)現(xiàn)的從右到左字符串替換方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的從右到左字符串替換方法,涉及Python字符串遍歷、運(yùn)算、判斷、替換等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
python 實(shí)時(shí)得到cpu和內(nèi)存的使用情況方法
今天小編就為大家分享一篇python 實(shí)時(shí)得到cpu和內(nèi)存的使用情況方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過公共鍵對字典列表排序算法示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過公共鍵對字典列表排序算法,結(jié)合實(shí)例形式分析了Python基于operator模塊中的itemgetter()函數(shù)對字典進(jìn)行排序的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Python自動(dòng)化完成tb喵幣任務(wù)的操作方法
2019雙十一,tb推出了新的活動(dòng),商店喵幣,看了一下每天都有幾個(gè)任務(wù)來領(lǐng)取喵幣,從而升級店鋪賺錢,然而我既想賺紅包又不想干苦力,遂使用python來進(jìn)行手機(jī)自動(dòng)化操作,需要的朋友跟隨小編一起看看吧2019-10-10
python實(shí)現(xiàn)自動(dòng)登錄后臺管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)自動(dòng)登錄后臺管理系統(tǒng),并進(jìn)行后續(xù)操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
python GUI庫圖形界面開發(fā)之PyQt5滾動(dòng)條控件QScrollBar詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5滾動(dòng)條控件QScrollBar詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03

