student management system
3.1 Mission Requirements
-
Implement a student class, including basic information such as student ID, name, gender, age, and place of origin.
-
Develop a system that can input the basic information of n students and save them in a file; can query, modify, and delete student information.
-
Implement a sorting algorithm that can sort the output of n students.
3.2 System Design
1. First create a student class Student, define private properties and private methods in the Student class, use the __slots__ variable to include all private properties, and define the student number, name, gender, age, and place of origin as private properties, which cannot be dynamically Add and delete the attributes of the class; assign values to the attributes in the initialization method, and the initialization method will be called every time an object is created. The __str__() method is rewritten inside the Student class, and the __str__() method is displayed when the object is directly printed. For the content inside, Student uses the @property decorator, which is used before the function declaration. The name of the method is a property. In the future, the method can be called without the method form, but in the attribute form. To "save" data, You can add @xxx.setter in front of another method to represent the "assignment" to the property through this method.
2. The system uses the tkinter library to implement the UI interface. The interface is mainly divided into multi-functional interfaces such as login, selection, adding students, deleting students, modifying students, querying a single student, querying multiple students, and exiting.
Login interface: Use the Canvas class in the tkinter library to create a canvas, then use the PhotoImage method to import the image, and finally use the create_image() method in the Canvas class to put the image into the canvas, and import the image into the interface; at the same time, use Two Label labels display User name and Password, each Label corresponds to an input box, get the content entered by the user in the input box, match the content in the file, and the match is successful, then you can enter the system, if the password is wrong, the messagebox will pop up , it shows that the entered password does not match the user name, and you can also register
Selection interface: When the user enters the correct user name and password, it will enter the selection interface. There are six buttons in the selection interface, namely add, delete, modify, query a single student, query all students and exit. When the user clicks the corresponding button to enter the corresponding interface.
Add student: In the add student interface, the user can enter the student's student number, name, gender, age, place of origin and other information, obtain the value in this Entry to construct the corresponding student class object, and store it through a dictionary. The key of the dictionary is the student's Student ID, because the student ID is unique, and the value is a class object. When the student ID of the added student exists, a messagebox message will pop up, prompting the user that the student already exists.
Delete student: Enter the relevant information of the student, delete the student information from the dictionary, if the student does not exist, a pop-up message will show that the student does not exist.
Modify student: Enter the relevant information of the student to be modified. If the student does not exist, a pop-up message will prompt that the user does not exist. If the user exists, delete the information about the student in the dictionary first, and then add the modified information.
Query the information of a single student: Enter the student ID of the student. If the student ID does not exist, the user will be prompted that the student ID does not exist. If it exists, the student's information will be displayed in the text box.
Query the information of all students: traverse the entire dictionary, and display all the values of the dictionary in the text box. In the query interface, you can also sort all the information according to age.
related functions:
- Usr_login() function
Role: to achieve user login
Parameters: none
- usr_sign_up function
Role: to achieve user registration
Parameters: window, where window is the main interface for user login
- options() function
Role: Implement an operation that the user can choose
Parameters: window, where window is the main interface for user login
- insert_student() function
Function: Realize user's adding operation to students
Parameters: window_option, where window_option is the user's selection interface
- remove_student() function
Function: realize the user's delete operation on students
Parameters: window_option, where window_option is the user's selection interface
- modify_student() function
Function: Realize user's modification operation on students
Parameters: window_option, where window_option is the user's selection interface
- search_student() function
Function: realize the user's query operation on a single student
Parameters: window_option, where window_option is the user's selection interface
- show_all() function
Role: realize the user's query operation on all students
Parameters: window_option, where window_option is the user's selection interface
- save_file() function
Function: Enable users to save student information to a file
Parameters: none
- load_file() function
Role: Realize the user to read the student's information in the file
Parameters: none
3.3 System Implementation and Operation Results
The login interface of the student management system mainly includes a Welcome icon, as well as an input box for the user to input the user name and password, which can perform input and output operations. Figure 4 is the login interface of the student management system.
Figure 4 Student management system login interface
The selection interface of the student management system consists of 6 buttons. Users can perform operations such as adding students, deleting students, modifying students, querying a single student, viewing all student information, and exiting the interface. Figure 5 shows the selection interface.
Figure 5 Selection interface of student management system
The interface for adding student information includes the input of student ID, name, gender, age, place of origin and other information. Figure 6 is the adding interface of the student management system.
Figure 6 Add student interface of student management system
The delete student information interface includes the input of student ID, name, gender, age, place of origin and other information. Figure 7 is the deletion interface of the student management system.
Figure 7 Delete interface of student management system
The interface for modifying student information includes the input of student ID, name, gender, age, place of origin and other information. Figure 8 is the modification interface of the student management system.
Figure 8 Modify interface of student management system
The interface for querying individual student information includes entering the student number of the student, clicking the OK button to query, and then displaying the relevant student information in the text box. Figure 9 is an interface for querying individual student information in the student management system.
Figure 9 Querying single student information in the student management system
Query all student information interface A text box that displays student information, including submit, sort (sort by age), cancel and other buttons, you can perform related operations. Figure 10 is the interface for querying all student information in the student management system
Figure 10 Query all student information interface
The interface after sorting is shown in Figure 11
Figure 11 The interface after sorting all students in the student management system
Code
1.student class
class Student(object): # Create student class __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}" ########"Fetch" operation on attributes######### @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 #########Defining properties, "assignment" operations on properties######### @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 class
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: # Determine whether the student ID has a 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()) # put into dictionary stu_dicts[entry_usr_id.get()] = stu # put student object into dictionary key = data value save_file() show_display() def show_display(): #Show hidden windows window_option.deiconify() window_add.withdraw() window_option.withdraw() window_add = tk.Toplevel(window_option) window_add.title('Add student information') window_add.geometry('700x500') tk.Label(window_add, text='Please enter student information', font=('Arial', 12), width=15, height=2).place(x=320, y=80) tk.Label(window_add, text='student ID').place(x=300, y=140) tk.Label(window_add, text='Name').place(x=300, y=200) tk.Label(window_add, text='gender').place(x=300, y=260) tk.Label(window_add, text='age').place(x=300, y=320) tk.Label(window_add, text='Hometown').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='submit', width=5, height=2, command=student_information) button_yes.place(x=300, y=420) button_cancel = tk.Button(window_add, text='Cancel', width=5, height=2, command=show_display) button_cancel.place(x=420, y=420)
3.delete_student class
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): # Delete student information def student_information(): if entry_usr_id.get() in stu_dicts: # Determine whether the student ID has a 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(): # Show hidden windows window_option.deiconify() window_del.withdraw() window_option.withdraw() window_del = tk.Toplevel(window_option) window_del.title('Delete student information') window_del.geometry('700x500') tk.Label(window_del, text='Please enter student information', font=('Arial', 12), width=15, height=2).place(x=320, y=80) tk.Label(window_del, text='Please enter student information', font=('Arial', 12), width=15, height=2).place(x=320, y=80) tk.Label(window_del, text='student ID').place(x=300, y=140) tk.Label(window_del, text='Name').place(x=300, y=200) tk.Label(window_del, text='gender').place(x=300, y=260) tk.Label(window_del, text='age').place(x=300, y=320) tk.Label(window_del, text='Hometown').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='submit', width=5, height=2, command=student_information) button_yes.place(x=300, y=420) button_cancel = tk.Button(window_del, text='Cancel', width=5, height=2, command=show_display) button_cancel.place(x=420, y=420)
4.modify_student class
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): # Edit student information def student_information(): if entry_usr_id.get() not in stu_dicts: # Determine whether the student ID has a 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()) # put into dictionary stu_dicts[entry_usr_id.get()] = stu # put student object into dictionary key = data value save_file() show_display() def show_display(): # Show hidden windows window_option.deiconify() window_modify.withdraw() window_option.withdraw() window_modify = tk.Toplevel(window_option) window_modify.title('Edit student information') window_modify.geometry('700x500') tk.Label(window_modify, text='Please enter student information', font=('Arial', 12), width=15, height=2).place(x=320, y=80) tk.Label(window_modify, text='student ID').place(x=300, y=140) tk.Label(window_modify, text='Name').place(x=300, y=200) tk.Label(window_modify, text='gender').place(x=300, y=260) tk.Label(window_modify, text='age').place(x=300, y=320) tk.Label(window_modify, text='Hometown').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='submit', width=5, height=2, command=student_information) button_yes.place(x=300, y=420) button_cancel = tk.Button(window_modify, text='Cancel', 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('selection interface') window_option.geometry('700x500') def quit(window_option): window_option.withdraw() window.deiconify() #define tags label = tk.Label(window_option, text='select item', font=('Arial', 12), width=15, height=2) label.place(x=100, y=240) button_add = tk.Button(window_option, text='Add to', width=15, height=2, command=lambda: insert_student(window_option)) button_del = tk.Button(window_option, text='delete', width=15, height=2, command=lambda: remove_student(window_option)) button_modify = tk.Button(window_option, text='Revise', width=15, height=2, command=lambda: modify_student(window_option)) button_search = tk.Button(window_option, text='Query a single student', width=15, height=2, command=lambda: search_student(window_option)) button_show_all = tk.Button(window_option, text='Query all student information', width=15, height=2, command=lambda: show_all(window_option)) button_quit = tk.Button(window_option, text='quit', 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(): # Save student information to file f = open('student.txt', 'w', encoding='utf-8') for stu in stu_dicts.values(): f.write(str(stu) + "\n") # stu will call the class __tr__ f.close() def load_file(): # read file f = open("student.txt", 'r', encoding='utf-8') buf_list = f.readlines() for buf in buf_list: buf = buf.strip() # remove\n info_list = buf.split(',') # cut with comma stu = student.Student(*info_list) # Unpack the list information to get each data 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(): # Show hidden windows 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('Inquire about student information') window_search.geometry('700x500') tk.Label(window_search, text='Please enter student information', font=('Arial', 12), width=15, height=2).place(x=320, y=80) tk.Label(window_search, text='student ID').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='submit', width=5, height=2, command=student_information) button_yes.place(x=300, y=320) button_cancel = tk.Button(window_search, text='Cancel', 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('Inquire about student information') 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(): # Show hidden windows window_option.deiconify() window_search_all.withdraw() button_sort = tk.Button(window_search_all, text='sort', width=5, height=2, command=another_show_information) button_sort.place(x=360, y=720) button_yes = tk.Button(window_search_all, text='submit', width=5, height=2, command=show_information) button_yes.place(x=300, y=720) button_cancel = tk.Button(window_search_all, text='Cancel', 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('student management system') 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()