学生管理系统

3.1任务要求

  1. 实现一个学生类,包括学号,姓名,性别,年龄,籍贯等基本信息。

  2. 开发一个系统,能够输入n个学生的基本信息,并保存在文件中;能够查询、修改、删除学生信息。

  3. 实现一个排序算法,能够对n个学生进行排序输出。

3.2 系统设计

1.首先创建一个学生类Student,在Student类里定义私有属性和私有方法,使用__slots__变量包括所有的私有属性,将学号、姓名、性别、年龄、籍贯定义为私有属性,不能动态的增加和删除类的属性;在初始化方法里为属性赋值,每次创建一个对象的时候都会调用初始化方法,Student类内部重写了__str__()方法,直接打印对象的时候显示__str__()方法里面的内容,Student使用@property装饰器,在函数声明之前使用,代表这个方法的名字就是一个属性,以后对这个方法的调用就可以不用方法形式,而是使用属性形式,要“存”数据,可以在另一个方法前面加上@xxx.setter,代表通过这个方法对属性“赋值”。

2.本系统采用的是tkinter库实现UI界面,界面主要分为登录、选择、增添学生、删除学生、修改学生、查询单个学生、查询多个学生、退出等多功能界面。

登录界面:使用tkinter库里面的Canvas类创建了一个画布,然后使用PhotoImage方法将图片导入,最后使用Canvas类里面的create_image()方法将图片放入画布中,实现了图片导入到界面中;同时使用两个Label标签显示User name和Password,每个Label对应一个输入框,获取用户在输入框输入的内容,与文件中的内容进行匹配,匹配成功,方可进去系统,如果密码错误,则弹出messagebox,显示输入的密码与用户名不匹配,同时还可以可以进行注册

选择界面:当用户输入正确的用户名和密码,则会进入到选择界面,选择界面里有六个按钮,分别为添加、删除、修改、查询单个学生、查询全部学生和退出,当用户点击相应的按钮,会进入相应的界面。

添加学生:在添加学生界面,用户可以输入学生的学号、姓名、性别、年龄、籍贯等信息,获取这个Entry里面的值构建相应的学生类对象,通过字典进行存储,字典的键为学生的学号,因为学号是唯一的,值为类对象,当添加的学生的学号存在时,会弹出messagebox信息,提示用户该学生已经存在。

删除学生:输入学生的相关信息,从字典内删除学生信息,如果学生不存在,则弹出信息,显示该学生不存在。

修改学生: 输入需要修改的学生的相关信息,如果学生不存在,则弹出信息提示用户不存在,如果用户存在,则在字典中先删除有关该学生的信息,然后在添加修改之后的信息。

查询单个学生的信息:输入学生的学号,如果学生的学号不存在,则提示用户学号不存在,如果存在,则在文本框里显示学生的信息。

查询所有学生的信息:遍历整个字典,将字典所有的值都显示到文本框中,在查询界面还可以将所有的信息按照年龄进行牌序。

相关函数:

  1. Usr_login()函数

作用:实现用户的登录

参数:无

  1. usr_sign_up函数

作用:实现用户的注册

参数:window,其中window为用户登录的主界面

  1. options()函数

作用: 实现一个用户可以选择的操作

参数:window, 其中window为用户登录的主界面

  1. insert_student()函数

作用: 实现用户对学生的增添操作

参数:window_option,其中window_option为用户的选择界面

  1. remove_student()函数

作用:实现用户对学生的删除操作

参数:window_option,其中window_option为用户的选择界面

  1. modify_student()函数

作用:实现用户对学生的修改操作

参数:window_option,其中window_option为用户的选择界面

  1. search_student()函数

作用:实现用户对单个学生的查询操作

参数:window_option,其中window_option为用户的选择界面

  1. show_all()函数

作用:实现用户对所有学生的查询操作

参数:window_option,其中window_option为用户的选择界面

  1. save_file()函数

作用:实现用户将学生信息保存到文件中

参数:无

  1. load_file()函数

作用:实现用户读取文件中的学生的信息

参数:无

3.3 系统实现与运行结果

学生管理系统登录界面主要包括一个Welcome图标,还有用户输入用户名和密码的输入框,可以进行输入输出操作。图4为学生管理系统的登录界面。
​                                                 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MsFT988P-1664532100691)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps4.jpg)]
图4 学生管理系统登录界面

学生管理系统的选择界面由6个按钮组成,用户可以进行添加学生、删除学生、修改学生、查询单个学生、查阅全部学生信息和退出界面等操作。图5为选择界面。

​                                            [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hARE7GkT-1664532100692)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps5.jpg)]

图5 学生管理系统之选择界面

添加学生信息界面包括学生学号、姓名、性别、年龄、籍贯等信息的输入。图6为学生管理系统的添加界面。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5vNVON6T-1664532100692)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps6.jpg)]

图6 学生管理系统之添加学生界面

删除学生信息界面包括学生学号、姓名、性别、年龄、籍贯等信息的输入。图7为学生管理系统的删除界面。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N2lyw9Ot-1664532100693)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps7.jpg)]

图7 学生管理系统之删除界面

修改学生信息界面包括学生学号、姓名、性别、年龄、籍贯等信息的输入。图8为学生管理系统的修改界面。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XwseahD8-1664532100693)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps8.jpg)]

图8 学生管理系统之修改界面

查询单个学生信息界面包括输入学生的学号,点击确定按钮进行查询,然后会在文本框里显示相关学生的信息。图9为学生管理系统之查询单个学生信息界面。

在这里插入图片描述

图9 学生管理系统之查询单个学生信息

查询所有学生信息界面一个显示学生信息的文本框,包括提交、排序(按年龄进行排序)、取消等按钮,可以进行相关的操作。图10为学生管理系统之查询所有学生信息界面

在这里插入图片描述

图10 查询所有学生信息界面

进行排序后的界面如图11所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p8TwTCrI-1664532100695)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml21708\wps12.jpg)]

图11 学生管理系统之所有学生排序后界面

代码实现

1.student类

class Student(object):  # 创建学生类
    __slots__ = ('__stu_id', '__name', '__age', '__gender', '__native_place')

    def __init__(self, stu_id, name, gender, age, native_place):
        self.stu_id = stu_id
        self.name = name
        self.age = age
        self.gender = gender
        self.native_place = native_place

    def __str__(self):
        return f"{self.stu_id}, {self.name}, {self.age}, {self.gender}, {self.native_place}"


########对属性”取”操作#########
    @property
    def stu_id(self):
        return self.__stu_id

    @property
    def name(self):
        return self.__name

    @property
    def age(self):
        return self.__age

    @property
    def gender(self):
        return self.__gender

    @property
    def native_place(self):
        return self.__native_place

 #########定义属性,对属性”赋值”操作#########
    @stu_id.setter
    def stu_id(self, stu_id):
        self.__stu_id = stu_id

    @name.setter
    def name(self, name):
        self.__name = name

    @age.setter
    def age(self, age):
        self.__age = age

    @gender.setter
    def gender(self, gender):
        self.__gender = gender

    @native_place.setter
    def native_place(self, native_place):
        self.__native_place = native_place

2.add_student类

import tkinter as tk
from task.manager1 import student
from tkinter import messagebox
from task.manager1.sava_load import stu_dicts
from task.manager1.sava_load import save_file

def insert_student(window_option):
    def student_information():
        if entry_usr_id.get() in stu_dicts:  # 判断学生学号是否存在 key
            tk.messagebox.showwarning(message='This student_number has exited')
            return
        if len(entry_usr_id.get()) != 11:
            tk.messagebox.showwarning(message='This length of student_number is wrong, please try again!')
            return
        if not entry_usr_id.get().isdigit():
            tk.messagebox.showwarning(message='The number of student_number should be a number, please try again!')
            return
        stu = student.Student(entry_usr_id.get(), entry_usr_name.get(), entry_usr_gender.get(),
                              entry_usr_age.get(), entry_usr_place.get())  # 放入字典
        
        stu_dicts[entry_usr_id.get()] = stu  # 将学生对象放入字典的 key = 数据值
        save_file()
        show_display()
    
    def show_display():
        #显示隐藏的窗口
        window_option.deiconify()
        window_add.withdraw()
    
    window_option.withdraw()
    window_add = tk.Toplevel(window_option)
    window_add.title('添加学生信息')
    window_add.geometry('700x500')

    tk.Label(window_add, text='请输入学生相关信息', font=('Arial', 12), width=15, height=2).place(x=320, y=80)

    tk.Label(window_add, text='学号').place(x=300, y=140)
    tk.Label(window_add, text='姓名').place(x=300, y=200)
    tk.Label(window_add, text='性别').place(x=300, y=260)
    tk.Label(window_add, text='年龄').place(x=300, y=320)
    tk.Label(window_add, text='籍贯').place(x=300, y=380)

    var_id = tk.StringVar()
    entry_usr_id = tk.Entry(window_add, textvariable=var_id)
    entry_usr_id.place(x=350, y=140)

    var_name = tk.StringVar()
    entry_usr_name = tk.Entry(window_add, textvariable=var_name)
    entry_usr_name.place(x=350, y=200)

    var_gender = tk.StringVar()
    entry_usr_gender = tk.Entry(window_add, textvariable=var_gender)
    entry_usr_gender.place(x=350, y=260)

    var_age = tk.StringVar()
    entry_usr_age = tk.Entry(window_add, textvariable=var_age)
    entry_usr_age.place(x=350, y=320)

    var_place = tk.StringVar()
    entry_usr_place = tk.Entry(window_add, textvariable=var_place)
    entry_usr_place.place(x=350, y=380)

    button_yes = tk.Button(window_add, text='提交', width=5, height=2, command=student_information)
    button_yes.place(x=300, y=420)
    
    button_cancel = tk.Button(window_add, text='取消', width=5, height=2, command=show_display)
    button_cancel.place(x=420, y=420)

3.delete_student类

import tkinter as tk
from task.manager1.sava_load import stu_dicts
from tkinter import messagebox
from task.manager1.sava_load import save_file


def remove_student(window_option):  # 删除学生信息
   def student_information():
      if entry_usr_id.get() in stu_dicts:  # 判断学生学号是否存在 key
         del stu_dicts[entry_usr_id.get()]
         save_file()
      else:
         tk.messagebox.showwarning(message='The student is not exit')
      show_display()
   
   def show_display():
      # 显示隐藏的窗口
      window_option.deiconify()
      window_del.withdraw()
   
   window_option.withdraw()
   window_del = tk.Toplevel(window_option)
   window_del.title('删除学生信息')
   window_del.geometry('700x500')
   
   tk.Label(window_del, text='请输入学生相关信息', font=('Arial', 12), width=15, height=2).place(x=320, y=80)
   
   tk.Label(window_del, text='请输入学生相关信息', font=('Arial', 12), width=15, height=2).place(x=320, y=80)
   
   tk.Label(window_del, text='学号').place(x=300, y=140)
   tk.Label(window_del, text='姓名').place(x=300, y=200)
   tk.Label(window_del, text='性别').place(x=300, y=260)
   tk.Label(window_del, text='年龄').place(x=300, y=320)
   tk.Label(window_del, text='籍贯').place(x=300, y=380)
   
   var_id = tk.StringVar()
   entry_usr_id = tk.Entry(window_del, textvariable=var_id)
   entry_usr_id.place(x=350, y=140)
   
   var_name = tk.StringVar()
   entry_usr_name = tk.Entry(window_del, textvariable=var_name)
   entry_usr_name.place(x=350, y=200)
   
   var_gender = tk.StringVar()
   entry_usr_gender = tk.Entry(window_del, textvariable=var_gender)
   entry_usr_gender.place(x=350, y=260)
   
   var_age = tk.StringVar()
   entry_usr_age = tk.Entry(window_del, textvariable=var_age)
   entry_usr_age.place(x=350, y=320)
   
   var_place = tk.StringVar()
   entry_usr_place = tk.Entry(window_del, textvariable=var_place)
   entry_usr_place.place(x=350, y=380)
   
   button_yes = tk.Button(window_del, text='提交', width=5, height=2, command=student_information)
   button_yes.place(x=300, y=420)
   
   button_cancel = tk.Button(window_del, text='取消', width=5, height=2, command=show_display)
   button_cancel.place(x=420, y=420)

4.modify_student类

import tkinter as tk
from task.manager1 import student
from tkinter import messagebox
from task.manager1.sava_load import stu_dicts
from task.manager1.sava_load import save_file

def modify_student(window_option):  # 修改学生信息
	def student_information():
		if entry_usr_id.get() not in stu_dicts:  # 判断学生学号是否存在 key
			tk.messagebox.showwarning(message='This student_number does not exited')
			return
		
		else:
			del stu_dicts[entry_usr_id.get()]
			stu = student.Student(entry_usr_id.get(), entry_usr_name.get(), entry_usr_gender.get(),
		                      entry_usr_age.get(), entry_usr_place.get())  # 放入字典
		
			stu_dicts[entry_usr_id.get()] = stu  # 将学生对象放入字典的 key = 数据值
			save_file()
			show_display()
	
	def show_display():
		# 显示隐藏的窗口
		window_option.deiconify()
		window_modify.withdraw()
	
	window_option.withdraw()
	window_modify = tk.Toplevel(window_option)
	window_modify.title('修改学生信息')
	window_modify.geometry('700x500')
	
	tk.Label(window_modify, text='请输入学生相关信息', font=('Arial', 12), width=15, height=2).place(x=320, y=80)
	
	tk.Label(window_modify, text='学号').place(x=300, y=140)
	
	tk.Label(window_modify, text='姓名').place(x=300, y=200)
	tk.Label(window_modify, text='性别').place(x=300, y=260)
	tk.Label(window_modify, text='年龄').place(x=300, y=320)
	tk.Label(window_modify, text='籍贯').place(x=300, y=380)
	
	var_id = tk.StringVar()
	entry_usr_id = tk.Entry(window_modify, textvariable=var_id)
	entry_usr_id.place(x=350, y=140)
	
	var_name = tk.StringVar()
	entry_usr_name = tk.Entry(window_modify, textvariable=var_name)
	entry_usr_name.place(x=350, y=200)
	
	var_gender = tk.StringVar()
	entry_usr_gender = tk.Entry(window_modify, textvariable=var_gender)
	entry_usr_gender.place(x=350, y=260)
	
	var_age = tk.StringVar()
	entry_usr_age = tk.Entry(window_modify, textvariable=var_age)
	entry_usr_age.place(x=350, y=320)
	
	var_place = tk.StringVar()
	entry_usr_place = tk.Entry(window_modify, textvariable=var_place)
	entry_usr_place.place(x=350, y=380)
	
	button_yes = tk.Button(window_modify, text='提交', width=5, height=2, command=student_information)
	button_yes.place(x=300, y=420)
	
	button_cancel = tk.Button(window_modify, text='取消', width=5, height=2, command=show_display)
	button_cancel.place(x=420, y=420)

5.option

import tkinter as tk
from task.manager1.add_student import insert_student
from task.manager1.delete_student import remove_student
from task.manager1.modify_student import modify_student
from task.manager1.search_student import search_student
from task.manager1.show_all_info import show_all

def options(window):
    window.withdraw()
    window_option = tk.Toplevel(window)
    window_option.title('选择界面')
    window_option.geometry('700x500')

    def quit(window_option):
        window_option.withdraw()
        window.deiconify()

    #定义标签
    label = tk.Label(window_option, text='选择项目', font=('Arial', 12), width=15, height=2)
    label.place(x=100, y=240)

    button_add = tk.Button(window_option, text='添加', width=15, height=2, command=lambda: insert_student(window_option))
    button_del = tk.Button(window_option, text='删除', width=15, height=2, command=lambda: remove_student(window_option))
    button_modify = tk.Button(window_option, text='修改', width=15, height=2, command=lambda: modify_student(window_option))
    button_search = tk.Button(window_option, text='查询单个学生', width=15, height=2, command=lambda: search_student(window_option))
    button_show_all = tk.Button(window_option, text='查询全部学生信息', width=15, height=2, command=lambda: show_all(window_option))
    button_quit = tk.Button(window_option, text='退出', width=15, height=2, command=lambda: quit(window_option))
    
   
    button_add.place(x=300, y=120)
    button_del.place(x=500, y=120)
    button_modify.place(x=300, y=240)
    button_search.place(x=500, y=240)
    button_show_all.place(x=300, y=360)
    button_quit.place(x=500, y=360)
    

6.save_load

from task.manager1 import student
stu_dicts = {}


def save_file():  # 保存学生信息到文件
   f = open('student.txt', 'w', encoding='utf-8')
   for stu in stu_dicts.values():
      f.write(str(stu) + "\n")  # stu会调用类__tr__
   f.close()


def load_file():  # 读取文件
   f = open("student.txt", 'r', encoding='utf-8')
   buf_list = f.readlines()
   for buf in buf_list:
      buf = buf.strip()  # 去除\n
      info_list = buf.split(',')  # 用逗号切割
      stu = student.Student(*info_list)  # 列表信息拆包 得到每个数据
      stu_id = info_list[0]
      stu_dicts[stu_id] = stu
      
   f.close()

7、search_student

import tkinter as tk
from tkinter import messagebox
from task.manager1.sava_load import stu_dicts
from task.manager1.sava_load import load_file

load_file()
def search_student(window_option):
   def student_information():
      stu_id = entry_usr_id.get()
      if stu_id not in stu_dicts:
         tk.messagebox.showwarning(message='This student_number does not exited')
      else:
         show_message(window_search)
         
   
   def show_display():
      # 显示隐藏的窗口
      window_option.deiconify()
      window_search.withdraw()
   def show_message(window_search):
      var = stu_dicts[entry_usr_id.get()]
      t = tk.Text(window_search, height=4)
      t.insert('end', var)
      t.place(x=200, y=200)

   window_option.withdraw()
   window_search = tk.Toplevel(window_option)
   window_search.title('查询学生信息')
   window_search.geometry('700x500')
   
   tk.Label(window_search, text='请输入学生相关信息', font=('Arial', 12), width=15, height=2).place(x=320, y=80)
   
   tk.Label(window_search, text='学号').place(x=300, y=140)
   
   var_id = tk.StringVar()
   entry_usr_id = tk.Entry(window_search, textvariable=var_id)
   entry_usr_id.place(x=350, y=140)
   
   button_yes = tk.Button(window_search, text='提交', width=5, height=2, command=student_information)
   button_yes.place(x=300, y=320)

   button_cancel = tk.Button(window_search, text='取消', width=5, height=2, command=show_display)
   button_cancel.place(x=420, y=320)

8、show_all_info

import tkinter as tk
from task.manager1.sava_load import load_file
from task.manager1.sava_load import stu_dicts


load_file()
def show_all(window_option):
   window_option.withdraw()
   window_search_all = tk.Toplevel(window_option)
   window_search_all.title('查询学生信息')
   window_search_all.geometry('1000x800')
   t = tk.Text(window_search_all, height=30, width=400)
   t.place(x=200, y=200)
   
   def show_information():
      for value in stu_dicts.values():
         t.insert('end', value)
         t.insert('end', '\n')
         
   def another_show_information():
      t.delete('1.0', 'end')
      stu_dict1 = stu_dicts.copy()
      stu_dict1 = dict(sorted(stu_dict1.items(), key=lambda x: x[1].stu_id, reverse=True))
      for item in stu_dict1.items():
         t.insert('end', item[1])
         t.insert('end', '\n')
      
   def show_display():
      # 显示隐藏的窗口
      window_option.deiconify()
      window_search_all.withdraw()
   
   button_sort = tk.Button(window_search_all, text='排序', width=5, height=2, command=another_show_information)
   button_sort.place(x=360, y=720)
      
   button_yes = tk.Button(window_search_all, text='提交', width=5, height=2, command=show_information)
   button_yes.place(x=300, y=720)
   
   button_cancel = tk.Button(window_search_all, text='取消', width=5, height=2, command=show_display)
   button_cancel.place(x=420, y=720)

9.main

```student
import tkinter as tk
import pickle
from tkinter import messagebox
from task.manager1.option import options


window = tk.Tk()
window.title('学生管理系统')
window.geometry('700x400')

#welcome image
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='download.png')
image = canvas.create_image(80, 0, anchor='nw', image=image_file)
canvas.pack(side='top')

#user information
tk.Label(window, text='User name').place(x=180, y=170)
tk.Label(window, text='Password').place(x=180, y=220)

var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=340, y=170)


var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=340, y=220)


def usr_login():
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    try:
        with open('usrs_info.txt', 'rb') as usr_file:
            usrs_info = pickle.load(usr_file)
    except FileNotFoundError:
        with open('usrs_info.txt', 'wb') as usr_file:
            usrs_info = {'admin': 'admin'}
            pickle.dump(usrs_info, usr_file)
    if usr_name in usrs_info:
        if usr_pwd == usrs_info[usr_name]:
            tk.messagebox.showinfo(title="Welcome", message='How are you ' + usr_name)
            options(window)
        else:
            tk.messagebox.showerror(message='Error, your password is wrong, try again')
    else:
        is_sign_up = tk.messagebox.askyesno('Welcome',
                                            'You have not sign up yet, Sing up today?')
        if is_sign_up:
            usr_sign_up()


def usr_sign_up(window):
    def sign_to():
        np = new_pwd.get()
        npf = new_pwd_confirm.get()
        nn = new_name.get()
        with open('usrs_info.txt', 'rb') as urs_file:
            exit_usr_info = pickle.load(urs_file)

        if np != npf:
            tk.messagebox.showerror(title='Error', message='Password and confirm password must be the same')
        elif nn in exit_usr_info:
            tk.messagebox.showerror(title='Error', message='The user has already signed up')
        else:
            exit_usr_info[nn] = np
            with open('usrs_info.txt', 'wb') as urs_file:
                pickle.dump(exit_usr_info, urs_file)
            tk.messagebox.showinfo(message="Welcome, You have successfully signed up")
            window_sign_up.destroy()

    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('Sign up window')

    new_name = tk.StringVar()
    tk.Label(window_sign_up, text='User name:').place(x=10, y=10)
    entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
    entry_new_name.place(x=150, y=10)

    new_pwd = tk.StringVar()
    tk.Label(window_sign_up, text='Password:').place(x=10, y=50)
    entry_new_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
    entry_new_pwd.place(x=150, y=50)

    new_pwd_confirm = tk.StringVar()
    tk.Label(window_sign_up, text='Confirm Password:').place(x=10, y=90)
    entry_new_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
    entry_new_pwd_confirm.place(x=150, y=90)

    btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to)
    btn_comfirm_sign_up.place(x=150, y=130)
  



btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=240, y=280)
btn_sign_up = tk.Button(window, text='Sign up', command=lambda: usr_sign_up(window))
btn_sign_up.place(x=370, y=280)

window.mainloop()



Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐