import tkinter.filedialog
from tkinter import *
from tkinter import messagebox
a=Tk()
a.title('学生成绩管理系统')
a.geometry('300x150')
Label(text='账号:').place(x=50,y=30)
uname=Entry(a)
uname.place(x=100,y=30)
Label(text='密码:').place(x=50,y=70)
pwd=Entry(a)
pwd.place(x=100,y=70)
def login():
n=uname.get()
p=pwd.get()
if n=='123'and p=='456':
messagebox.showinfo('登陆成功','学生成绩管理系统')
a.destroy()
a2=Tk()
a2.title('学生成绩管理系统')
a2.geometry('600x300')
L1=Label(a2,text='欢迎进入学生成绩管理系统').place(x=225,y=125)
def p1():#录入
a2.destroy()
a3=Tk()
a3.title('学生成绩管理系统')
a3.geometry('600x300')
Label(a3,text='姓名:').place(x=50,y=30)
xm_text = StringVar()
xm=Entry(a3,textvariable = xm_text).place(x=150,y=30)
xm_text.set('')
Label(a3,text='班级:').place(x=50,y=50)
bj_text = StringVar()
bj=Entry(a3,textvariable = bj_text).place(x=150,y=50)
bj_text.set('')
Label(a3,text='学号:').place(x=50,y=70)
xh_text = StringVar()
xh=Entry(a3,textvariable = xh_text).place(x=150,y=70)
xh_text.set('')
Label(a3,text='高等数学成绩:').place(x=50,y=90)
c1_text = StringVar()
c1=Entry(a3,textvariable = c1_text).place(x=150,y=90)
c1_text.set('')
Label(a3,text='大学物理成绩:').place(x=50,y=110)
c2_text = StringVar()
c2=Entry(a3,textvariable = c2_text).place(x=150,y=110)
c2_text.set('')
Label(a3,text='理论力学成绩:').place(x=50,y=130)
c3_text = StringVar()
c3=Entry(a3,textvariable = c3_text).place(x=150,y=130)
c3_text.set('')
Label(a3,text='大学英语成绩:').place(x=50,y=150)
c4_text = StringVar()
c4=Entry(a3,textvariable = c4_text).place(x=150,y=150)
c4_text.set('')
def get_click():
xm=xm_text.get()
bj=bj_text.get()
xh=xh_text.get()
c1=c1_text.get()
c2=c2_text.get()
c3=c3_text.get()
c4=c4_text.get()
string = str('姓名:%s 班级 %s 学号: %s 高等数学成绩: %s 大学物理成绩: %s 理论力学成绩: %s 大学英语成绩: %s' %(xm, bj, xh, c1, c2, c3, c4))
print('姓名:%s 班级 %s 学号: %s 高等数学成绩: %s 大学物理成绩: %s 理论力学成绩: %s 大学英语成绩: %s' %(xm, bj, xh, c1, c2, c3, c4))
with open('学生成绩.txt','a') as f:
f.write('姓名:%s\t班级:%s\t学号:%s\t高等数学成绩:%s\t大学物理成绩:%s\t理论力学成绩:%s\t大学英语成绩:%s\n'%(xm,bj,xh,c1,c2,c3,c4))
messagebox.showinfo(title='录入成功', message = string)
Button(a3,text='确定',command=get_click).place(x=150,y=200)
Button(a3,text='返回',command=a3.destroy).place(x=250,y=180)
a3.mainloop()
def p2():#查询
a4=Tk()
a4.title('学生成绩管理系统---信息查询')
a4.geometry('600x300')
sid=StringVar(a4,value='')
Label(a4,text='请输入查询学生的姓名').place(x=225,y=30)
xmcx_1 = StringVar()
xmcx=Entry(a4,textvariable = xmcx_1).place(x=225,y=70)
xmcx_1.set('')
Label(a4,text='请输入查询学生的学号').place(x=225,y=130)
xhcx_1 = StringVar()
xhcx=Entry(a4,textvariable = xhcx_1).place(x=225,y=170)
xhcx_1.set('')
def select():
name=xmcx_1.get()
ID=xhcx_1.get()
with open('学生成绩.txt','r')as f:
for line in f:
if name in line:
if ID in line:
messagebox.showinfo(title='aaa', message = line)
else:
messagebox.showinfo(title='aaa', message = '查无此人')
Button(a4,text='查询',command=select).place(x=250,y=200)
Button(a4,text='返回',command=a4.destroy).place(x=300,y=200)
def p3():#删除
a5=Tk()
a5.title('学生成绩管理系统---删除')
a5.geometry('600x300')
Label(a5,text='请输入要删除学生的姓名').place(x=225,y=30)
xmcx_2 = StringVar()
xmcx=Entry(a5,textvariable = xmcx_2).place(x=225,y=70)
xmcx_2.set('')
Label(a5,text='请输入要删除学生的学号').place(x=225,y=130)
xhcx_2 = StringVar()
xhcx=Entry(a5,textvariable = xhcx_2).place(x=225,y=170)
xhcx_2.set('')
def delete():
name=xmcx_2.get()
ID=xhcx_2.get()
with open('学生成绩.txt','r')as f:
for line in f:
if name and ID not in line:
messagebox.showinfo(title='错误',message='查无此人,删除失败')
else:
with open('学生成绩.txt','w')as f:
if name and ID in line:
f.write(line.replace(name,ID))
messagebox.showinfo(title='成功',message='删除成功')
Button(a5,text='删除',command=delete).place(x=250,y=200)
Button(a5,text='返回',command=a5.destroy).place(x=300,y=200)
def p4():#修改
a6=Tk()
a6.title('学生成绩管理系统---修改')
a6.geometry('600x300')
Label(a6,text='请输入要修改学生的姓名').place(x=225,y=30)
xmcx_3 = StringVar()
xmcx=Entry(a6,textvariable = xmcx_3).place(x=225,y=70)
xmcx_3.set('')
Label(a6,text='请输入要修改学生的学号').place(x=225,y=130)
xhcx_3 = StringVar()
xhcx=Entry(a6,textvariable = xhcx_3).place(x=225,y=170)
xhcx_3.set('')
def update():
name=xmcx_3.get()
ID=xhcx_3.get()
with open('学生成绩.txt','r')as f:
for line in f:
if name in line:
if ID in line:
with open('学生成绩.txt','w')as f:
f.seek(0)
f.truncate()
else:
messagebox.showinfo(title='aaa', message = '查无此人')
Button(a6,text='修改',command=p1).place(x=250,y=200)
Button(a6,text='返回',command=a6.destroy).place(x=300,y=200)
top=Menu(a2)
top.add_radiobutton(label='录入',command=p1)
#Label(text='账号:').place(x=50,y=30)
top.add_radiobutton(label='查询',command=p2)
top.add_radiobutton(label='删除',command=p3)
top.add_radiobutton(label='修改',command=p4)
a2.config(menu=top)
a2.mainloop()
else:
messagebox.showinfo(title='错误', message = '请输入账号:123,密码:456')
Button(a,text='登录',command=login).place(x=100,y=100)
这段代码运行有几个问题:
1.录入板块如果删除a2.destroy()就会无法正常录入
录入内容呈现为:姓名: 班级 学号: 高等数学成绩: 大学物理成绩: 理论力学成绩: 大学英语成绩:
没有输入的内容,希望实现能够通过按返回按钮回到a2主界面
2.删除总是删掉全部内容,而不是指定学生信息
3.修改无法运行
首先需要对现有的代码进行分析,确定需要修改的位置和代码逻辑。以下是我对每个问题的解决方案:
// 判断输入的内容是否为空,如果为空,则提示用户必须输入
if(nameInput.value==""||mathInput.value==""||physicsInput.value==""||mechanicsInput.value==""||englishInput.value==""){
alert("必须输入姓名及各科成绩信息");
return;
}
// 将输入框中的内容保存为对象形式,方便存储
var student={
name:nameInput.value,
class:classInput.value,
number:numberInput.value,
math:parseFloat(mathInput.value),
physics:parseFloat(physicsInput.value),
mechanics:parseFloat(mechanicsInput.value),
english:parseFloat(englishInput.value),
};
// 将对象添加到数组中
students.push(student);
// 清空输入框内容
nameInput.value="";
classInput.value="";
numberInput.value="";
mathInput.value="";
physicsInput.value="";
mechanicsInput.value="";
englishInput.value="";
// 提示用户数据录入成功
alert("数据录入成功!");
// 获取输入框中的内容
var delContent = deleteInput.value;
// 保存不需要删除的学生信息
var newStudents = [];
// 遍历数组,将符合条件的学生信息从数组中删除
students.forEach(function(item){
if(!(item.name==delContent||item.class==delContent||item.number==delContent)){
newStudents.push(item);
}
});
// 将新的数组赋值给原数组
students=newStudents;
// 提示用户删除成功
alert("删除成功!");
// 保存需要修改的学生信息
var needModStudent=[];
// 根据用户输入的内容查找相应的学生信息
students.forEach(function(item){
if(item.name==modContent||item.class==modContent||item.number==modContent){
needModStudent.push(item);
}
});
// 根据用户选择的信息,在页面上显示出该学生的信息
var modStudent=needModStudent[0];//可通过下拉框选择修改的数据具体是哪一个数据
nameInput.value=modStudent.name;
classInput.value=modStudent.class;
numberInput.value=modStudent.number;
mathInput.value=modStudent.math;
physicsInput.value=modStudent.physics;
mechanicsInput.value=modStudent.mechanics;
englishInput.value=modStudent.english;
然后,可以根据用户在页面上修改的内容,更新该学生的数据:
// 保存修改后的结果
var modResult={
name:nameInput.value,
class:classInput.value,
number:numberInput.value,
math:parseFloat(mathInput.value),
physics:parseFloat(physicsInput.value),
mechanics:parseFloat(mechanicsInput.value),
english:parseFloat(englishInput.value)
};
// 遍历数组,查找到需要修改的学生信息,并进行修改
students.forEach(function(item,index){
if(item.name==modContent||item.class==modContent||item.number==modContent){
students.splice(index,1,modResult);
}
});
// 提示用户修改成功
alert("修改成功!");
以上是我对每个问题的解决方案,希望能够对您有所帮助!