用Python编写一个学生信息管理系统(姓名、学号、联系方式、绩点),主要包含两个界面:登录界面和学生信息管理系统,学生信息管理系统主要功能是实现学生信息的增删改查。Python的图形界面GUI工具有很多,Tkinter和PyQt5可以任选其一。
时间: 2023-06-26 08:08:17 浏览: 184
好的,下面是一个基于Tkinter的学生信息管理系统的代码示例:
```python
import tkinter as tk
class StudentInfo:
def __init__(self, name, id, contact, score):
self.name = name
self.id = id
self.contact = contact
self.score = score
class StudentInfoManager:
def __init__(self):
self.student_infos = []
def add_student_info(self, student_info):
self.student_infos.append(student_info)
def remove_student_info(self, id):
for student_info in self.student_infos:
if student_info.id == id:
self.student_infos.remove(student_info)
return True
return False
def update_student_info(self, id, new_student_info):
for i, student_info in enumerate(self.student_infos):
if student_info.id == id:
self.student_infos[i] = new_student_info
return True
return False
def find_student_info(self, id):
for student_info in self.student_infos:
if student_info.id == id:
return student_info
return None
class LoginPanel(tk.Frame):
def __init__(self, parent, student_info_manager, on_login):
super().__init__(parent)
self.student_info_manager = student_info_manager
self.on_login = on_login
self.username_label = tk.Label(self, text="Username:")
self.username_entry = tk.Entry(self)
self.password_label = tk.Label(self, text="Password:")
self.password_entry = tk.Entry(self, show="*")
self.login_button = tk.Button(self, text="Login", command=self.login)
self.username_label.pack()
self.username_entry.pack()
self.password_label.pack()
self.password_entry.pack()
self.login_button.pack()
def login(self):
if self.username_entry.get() == "admin" and self.password_entry.get() == "admin":
self.on_login()
else:
tk.messagebox.showerror("Error", "Incorrect username or password")
class StudentInfoPanel(tk.Frame):
def __init__(self, parent, student_info_manager):
super().__init__(parent)
self.student_info_manager = student_info_manager
self.id_label = tk.Label(self, text="ID:")
self.id_entry = tk.Entry(self)
self.name_label = tk.Label(self, text="Name:")
self.name_entry = tk.Entry(self)
self.contact_label = tk.Label(self, text="Contact:")
self.contact_entry = tk.Entry(self)
self.score_label = tk.Label(self, text="Score:")
self.score_entry = tk.Entry(self)
self.add_button = tk.Button(self, text="Add", command=self.add_student_info)
self.remove_button = tk.Button(self, text="Remove", command=self.remove_student_info)
self.update_button = tk.Button(self, text="Update", command=self.update_student_info)
self.find_button = tk.Button(self, text="Find", command=self.find_student_info)
self.id_label.pack()
self.id_entry.pack()
self.name_label.pack()
self.name_entry.pack()
self.contact_label.pack()
self.contact_entry.pack()
self.score_label.pack()
self.score_entry.pack()
self.add_button.pack()
self.remove_button.pack()
self.update_button.pack()
self.find_button.pack()
def add_student_info(self):
student_info = StudentInfo(self.name_entry.get(), self.id_entry.get(), self.contact_entry.get(), self.score_entry.get())
self.student_info_manager.add_student_info(student_info)
tk.messagebox.showinfo("Info", "Student info added successfully")
def remove_student_info(self):
success = self.student_info_manager.remove_student_info(self.id_entry.get())
if success:
tk.messagebox.showinfo("Info", "Student info removed successfully")
else:
tk.messagebox.showerror("Error", "Student info not found")
def update_student_info(self):
new_student_info = StudentInfo(self.name_entry.get(), self.id_entry.get(), self.contact_entry.get(), self.score_entry.get())
success = self.student_info_manager.update_student_info(self.id_entry.get(), new_student_info)
if success:
tk.messagebox.showinfo("Info", "Student info updated successfully")
else:
tk.messagebox.showerror("Error", "Student info not found")
def find_student_info(self):
student_info = self.student_info_manager.find_student_info(self.id_entry.get())
if student_info:
self.name_entry.delete(0, tk.END)
self.contact_entry.delete(0, tk.END)
self.score_entry.delete(0, tk.END)
self.name_entry.insert(0, student_info.name)
self.contact_entry.insert(0, student_info.contact)
self.score_entry.insert(0, student_info.score)
else:
tk.messagebox.showerror("Error", "Student info not found")
class StudentInfoManagementSystem(tk.Tk):
def __init__(self):
super().__init__()
self.title("Student Info Management System")
self.geometry("400x300")
self.student_info_manager = StudentInfoManager()
self.login_panel = LoginPanel(self, self.student_info_manager, self.show_student_info_panel)
self.student_info_panel = StudentInfoPanel(self, self.student_info_manager)
self.login_panel.pack(fill=tk.BOTH, expand=True)
def show_student_info_panel(self):
self.login_panel.pack_forget()
self.student_info_panel.pack(fill=tk.BOTH, expand=True)
if __name__ == "__main__":
app = StudentInfoManagementSystem()
app.mainloop()
```
这个程序中,我们定义了一个`StudentInfo`类来表示学生信息,包含姓名、学号、联系方式和绩点四个属性。另外我们还定义了一个`StudentInfoManager`类来管理所有学生信息,包含增加、删除、修改和查找等操作。这些操作都是通过对`student_infos`列表进行操作实现的。
程序的图形界面包含两个部分,一个是登录界面`LoginPanel`,另一个是学生信息管理界面`StudentInfoPanel`。在登录界面中,我们使用了`tk.Entry`来输入用户名和密码,并在点击`Login`按钮时判断用户名和密码是否正确。如果正确,则调用`on_login`方法切换到学生信息管理界面;如果不正确,则弹出错误提示框。在学生信息管理界面中,我们使用了`tk.Entry`来输入学生信息的各个属性,并使用`tk.Button`来触发各种操作。其中,增加、删除、修改和查找操作都是通过调用`StudentInfoManager`中相应的方法实现的。在查找操作中,我们可以根据学号来查找学生信息,并在找到后把学生信息的各个属性填入对应的`tk.Entry`中。
以上就是一个简单的基于Tkinter的学生信息管理系统的代码示例。注意,这个示例只是一个参考,实际情况下可能需要根据具体需求进行修改。
阅读全文