全部代码:点击打开链接
from tkinter import *
from LoginPage import *
root = Tk()
root.title('Python成绩管理系统_by vision')
LoginPage(root)
root.mainloop()
from tkinter import *
from tkinter.messagebox import *
from MainPage import *
class LoginPage(object):
def __init__(self, master=None):
self.root = master #定义内部变量root
self.root.geometry('%dx%d' % (300, 180)) #设置窗口大小
self.username = StringVar()
self.password = StringVar()
self.createPage()
def createPage(self):
self.page = Frame(self.root) #创建Frame
self.page.pack()
Label(self.page).grid(row=0, stick=W)
Label(self.page, text = '账户: ').grid(row=1, stick=W, pady=10)
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E)
Label(self.page, text = '密码: ').grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E)
Button(self.page, text='登陆', command=self.loginCheck).grid(row=3, stick=W, pady=10)
Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E)
def loginCheck(self):
name = self.username.get()
password = self.password.get()
if name=='admin' and password=='admin':
self.page.destroy()
MainPage(self.root)
else:
showinfo(title='错误', message='账号或密码错误!')
from tkinter import *
from view import * #菜单栏对应的各个子页面
class MainPage(object):
def __init__(self, master=None):
self.root = master #定义内部变量root
self.root.geometry('%dx%d' % (600, 400)) #设置窗口大小
self.createPage()
def createPage(self):
self.inputPage = InputFrame(self.root) # 创建不同Frame
self.deletePage = DeleteFrame(self.root)
self.modifyPage = ModifyFrame(self.root)
self.queryPage = QueryFrame(self.root)
self.inputPage.pack() #默认显示数据录入界面
menubar = Menu(self.root)
menubar.add_command(label='增', command = self.inputData)
menubar.add_command(label='删', command = self.deleteData)
menubar.add_command(label='改', command = self.modifyData)
menubar.add_command(label='查', command = self.queryData)
self.root['menu'] = menubar # 设置菜单栏
def inputData(self):
self.inputPage.pack()
self.queryPage.pack_forget()
self.deletePage.pack_forget()
self.modifyPage.pack_forget()
def deleteData(self):
self.inputPage.pack_forget()
self.queryPage.pack_forget()
self.deletePage.pack()
self.modifyPage.pack_forget()
def modifyData(self):
self.inputPage.pack_forget()
self.queryPage.pack_forget()
self.deletePage.pack_forget()
self.modifyPage.pack()
def queryData(self):
self.inputPage.pack_forget()
self.queryPage.pack()
self.deletePage.pack_forget()
self.modifyPage.pack_forget()
from tkinter import *
from tkinter.messagebox import *
class InputFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.E1 = Entry(self)
self.E2 = Entry(self)
self.E3 = Entry(self)
self.E4 = Entry(self)
self.E5 = Entry(self)
self.createPage()
def Isspace(self,text):
temp = 0
for i in text:
if not i.isspace():
temp = 1
break;
if temp==1:
return 0
else:
return 1
def write(self,name,num,course,score):
f = open('成绩.csv','r',encoding='utf-8')
for line in f.readlines():
info = line[:-1].split(",")
if len(info)<4:
break
if info[1]==num and info[2]==course:
messagebox.showinfo(title='结果', message ="已存在该学生科目信息!")
f.close()
return
f.close()
f = open('成绩.csv','a',encoding='utf-8')
f.write('{},{},{},{}\n'.format(name,num,course,score))
f.close()
messagebox.showinfo(title='提示', message ="写入成功")
return
def click(self):
name = self.E1.get()
num = self.E2.get()
course = self.E3.get()
score = self.E4.get()
if self.Isspace(name) or self.Isspace(num) or self.Isspace(course) or self.Isspace(score) :
messagebox.showinfo(title='提示', message ="输入项为空")
else:
self.write(name,num,course,score)
def createPage(self):
Label(self).grid(row=0, stick=W, pady=10)
Label(self, text = '姓名: ').grid(row=1, stick=W, pady=10)
self.E1.grid(row=1, column=1, stick=E)
Label(self, text = '学号: ').grid(row=2, stick=W, pady=10)
self.E2.grid(row=2, column=1, stick=E)
Label(self, text = '科目: ').grid(row=3, stick=W, pady=10)
self.E3.grid(row=3, column=1, stick=E)
Label(self, text = '成绩: ').grid(row=4, stick=W, pady=10)
self.E4.grid(row=4, column=1, stick=E)
Button(self, text='录入',command=self.click).grid(row=6, column=1, stick=E, pady=10)
class DeleteFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.E1 = Entry(self)
self.E2 = Entry(self)
self.createPage()
def Isspace(self,text):
temp = 0
for i in text:
if not i.isspace():
temp = 1
break;
if temp==1:
return 0
else:
return 1
def delete(self,num,course):
temp = 0
with open("成绩.csv","r",encoding="utf-8") as f:
lines = f.readlines()
with open("成绩.csv","w",encoding="utf-8") as f_w:
for line in lines:
info = line[:-1].split(",")
if info[1]==num and info[2]==course:
temp = 1
continue
f_w.write(line)
if temp==0:
messagebox.showinfo(title='提示', message ="没有该信息")
else:
messagebox.showinfo(title='提示', message ="删除成功")
def click(self):
num = self.E1.get()
course = self.E2.get()
if self.Isspace(num) or self.Isspace(course):
messagebox.showinfo(title='提示', message ="输入项为空")
else:
self.delete(num,course)
def createPage(self):
Label(self).grid(row=0, stick=W, pady=10)
Label(self, text = '学号: ').grid(row=1, stick=W, pady=10)
self.E1.grid(row=1, column=1, stick=E)
Label(self, text = '科目: ').grid(row=2, stick=W, pady=10)
self.E2.grid(row=2, column=1, stick=E)
Button(self, text='删除',command=self.click).grid(row=6, column=1, stick=E, pady=10)
class ModifyFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.E1 = Entry(self)
self.E2 = Entry(self)
self.E3 = Entry(self)
self.E4 = Entry(self)
self.E5 = Entry(self)
self.createPage()
def Isspace(self,text):
temp = 0
for i in text:
if not i.isspace():
temp = 1
break;
if temp==1:
return 0
else:
return 1
def modify(self,name,num,course,score):
temp = 0
with open("成绩.csv","r",encoding="utf-8") as f:
lines = f.readlines()
with open("成绩.csv","w",encoding="utf-8") as f_w:
for line in lines:
info = line[:-1].split(",")
if info[1]==num and info[2]==course:
temp = 1
f_w.write('{},{},{},{}\n'.format(name,num,course,score))
continue
f_w.write(line)
if temp==0:
messagebox.showinfo(title='提示', message ="没有该信息")
else:
messagebox.showinfo(title='提示', message ="修改成功")
def click(self):
name = self.E1.get()
num = self.E2.get()
course = self.E3.get()
score = self.E4.get()
if self.Isspace(name) or self.Isspace(num) or self.Isspace(course) or self.Isspace(score) :
messagebox.showinfo(title='提示', message ="输入项为空")
else:
self.modify(name,num,course,score)
def createPage(self):
Label(self).grid(row=0, stick=W, pady=10)
Label(self, text = '姓名: ').grid(row=1, stick=W, pady=10)
self.E1.grid(row=1, column=1, stick=E)
Label(self, text = '学号: ').grid(row=2, stick=W, pady=10)
self.E2.grid(row=2, column=1, stick=E)
Label(self, text = '科目: ').grid(row=3, stick=W, pady=10)
self.E3.grid(row=3, column=1, stick=E)
Label(self, text = '成绩: ').grid(row=4, stick=W, pady=10)
self.E4.grid(row=4, column=1, stick=E)
Button(self, text='修改',command=self.click).grid(row=6, column=1, stick=E, pady=10)
class QueryFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.E1 = Entry(self)
self.E2 = Entry(self)
self.createPage()
def Isspace(self,text):
temp = 0
for i in text:
if not i.isspace():
temp = 1
break;
if temp==1:
return 0
else:
return 1
def query(self,num,course):
f = open('成绩.csv','r',encoding='utf-8')
for line in f.readlines():
info = line[:-1].split(",")
if info[1]==num and info[2]==course:
messagebox.showinfo(title='结果', message ="姓名:"+info[0]+"\n学号:"+info[1]+"\n科目:"+info[2]+"\n成绩:"+info[3])
f.close()
return
messagebox.showinfo(title='提示', message ="没有该信息")
f.close()
return
def click(self):
num = self.E1.get()
course = self.E2.get()
if self.Isspace(num) or self.Isspace(course):
messagebox.showinfo(title='提示', message ="输入项为空")
else:
self.query(num,course)
def createPage(self):
Label(self).grid(row=0, stick=W, pady=10)
Label(self, text = '学号: ').grid(row=1, stick=W, pady=10)
self.E1.grid(row=1, column=1, stick=E)
Label(self, text = '科目: ').grid(row=2, stick=W, pady=10)
self.E2.grid(row=2, column=1, stick=E)
Button(self, text='查找',command=self.click).grid(row=6, column=1, stick=E, pady=10)
学生1,001,语文,100
学生1,001,数学,100
学生2,002,语文,80
学生2,002,数学,100
学生3,003,语文,100
(最一行不要删!!!)