# 定义学生类
class Student:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
def __str__(self):
return 'Name: %s, Age: %s, Score: %s' % (self.name, self.age, self.score)
# 定义学生列表
student_list = []
# 定义功能函数
def show_menu():
print('*' * 20)
print('欢迎使用学生成绩登记系统')
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生')
print('4. 查询学生')
print('5. 显示所有学生')
print('6. 退出系统')
print('*' * 20)
def add_student():
name = input('请输入学生姓名:')
age = int(input('请输入学生年龄:'))
score = int(input('请输入学生成绩:'))
student = Student(name, age, score)
student_list.append(student)
print('添加%s成功!' % student.name)
def delete_student():
name = input('请输入要删除的学生姓名:')
for i in range(len(student_list)):
if student_list[i].name == name:
del student_list[i]
print('删除%s成功!' % name)
return
print('没有找到%s!' % name)
def modify_student():
name = input('请输入要修改的学生姓名:')
for i in range(len(student_list)):
if student_list[i].name == name:
student_list[i].age = int(input('请输入学生新年龄:'))
student_list[i].score = int(input('请输入学生新成绩:'))
print('修改%s成功!' % name)
return
print('没有找到%s!' % name)
def query_student():
name = input('请输入要查询的学生姓名:')
for i in range(len(student_list)):
if student_list[i].name == name:
print(student_list[i])
return
print('没有找到%s!' % name)
def show_all_student():
for student in student_list:
print(student)
# 主函数
def main():
while True:
show_menu()
choice = int(input('请输入你的操作:'))
if choice == 1:
add_student()
elif choice == 2:
delete_student()
elif choice == 3:
modify_student()
elif choice == 4:
query_student()
elif choice == 5:
show_all_student()
elif choice == 6:
break
else:
print('输入错误!')
if __name__ == '__main__':
main()
以下是增加了图形界面的代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
# 初始化Tk()
window = tk.Tk()
window.title('学生成绩登记系统')
window.geometry('400x300')
# 创建学生信息
l = tk.Label(window, text='学生信息', bg='green', font=('Arial', 12), width=15, height=2)
l.pack()
# 创建学生姓名输入框
tk.Label(window, text='学生姓名:', bg='yellow', font=('Arial', 12)).place(x=50, y=50)
var_name = tk.StringVar()
entry_name = tk.Entry(window, textvariable=var_name)
entry_name.place(x=160, y=50)
# 创建学生成绩输入框
tk.Label(window, text='学生成绩:', bg='yellow', font=('Arial', 12)).place(x=50, y=90)
var_score = tk.StringVar()
entry_score = tk.Entry(window, textvariable=var_score)
entry_score.place(x=160, y=90)
# 创建提交按钮
def insert_data():
name = var_name.get()
score = var_score.get()
print('学生姓名:%s,学生成绩:%s' % (name, score))
btn_confirm = tk.Button(window, text='提交', width=10, height=1, command=insert_data)
btn_confirm.place(x=160, y=130)
window.mainloop()