界面用的是wxpython
import wx
import pickle
class StudentInfoFrame(wx.Frame):
def __init__(self, parent, title):
super(StudentInfoFrame, self).__init__(parent, title=title, size=(600, 400))
self.panel = wx.Panel(self)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.students_list = []
# 学生信息输入区域
self.static_text_title = wx.StaticText(self.panel, label="学生信息管理系统", style=wx.ALIGN_CENTER)
self.static_text_title.SetFont(wx.Font(18, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
self.static_text_name = wx.StaticText(self.panel, label="姓名:")
self.text_ctrl_name = wx.TextCtrl(self.panel, size=(200, -1))
self.static_text_age = wx.StaticText(self.panel, label="年龄:")
self.text_ctrl_age = wx.TextCtrl(self.panel, size=(200, -1))
self.static_text_major = wx.StaticText(self.panel, label="专业:")
self.text_ctrl_major = wx.TextCtrl(self.panel, size=(200, -1))
self.btn_add = wx.Button(self.panel, label="添加学生")
self.btn_add.Bind(wx.EVT_BUTTON, self.on_add_student)
self.btn_delete = wx.Button(self.panel, label="删除选中学生")
self.btn_delete.Bind(wx.EVT_BUTTON, self.on_delete_student)
# 学生列表区域
self.list_ctrl_students = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list_ctrl_students.InsertColumn(0, '姓名', width=150)
self.list_ctrl_students.InsertColumn(1, '年龄', width=70)
self.list_ctrl_students.InsertColumn(2, '专业', width=200)
self.list_ctrl_students.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_select_student)
# 按钮区域
self.btn_save = wx.Button(self.panel, label="保存学生信息到文件")
self.btn_save.Bind(wx.EVT_BUTTON, self.on_save_info)
self.btn_load = wx.Button(self.panel, label="从文件加载学生信息")
self.btn_load.Bind(wx.EVT_BUTTON, self.on_load_info)
self.vbox.Add(self.static_text_title, flag=wx.ALL | wx.EXPAND, border=10)
self.vbox.Add(self.static_text_name, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.text_ctrl_name, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.static_text_age, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.text_ctrl_age, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.static_text_major, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.text_ctrl_major, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.btn_add, flag=wx.ALL | wx.CENTER, border=5)
self.vbox.Add(self.btn_delete, flag=wx.ALL | wx.CENTER, border=5)
self.vbox.Add(self.list_ctrl_students, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.btn_save, flag=wx.ALL | wx.CENTER, border=5)
self.vbox.Add(self.btn_load, flag=wx.ALL | wx.CENTER, border=5)
self.panel.SetSizer(self.vbox)
def on_add_student(self, event):
name = self.text_ctrl_name.GetValue()
age = self.text_ctrl_age.GetValue()
major = self.text_ctrl_major.GetValue()
if name and age and major:
self.students_list.append((name, age, major))
self.update_student_list()
self.text_ctrl_name.SetValue('')
self.text_ctrl_age.SetValue('')
self.text_ctrl_major.SetValue('')
def on_delete_student(self, event):
index = self.list_ctrl_students.GetFirstSelected()
if index != -1:
del self.students_list[index]
self.update_student_list()
def on_select_student(self, event):
index = event.GetIndex()
self.text_ctrl_name.SetValue(self.students_list[index][0])
self.text_ctrl_age.SetValue(self.students_list[index][1])
self.text_ctrl_major.SetValue(self.students_list[index][2])
def update_student_list(self):
self.list_ctrl_students.DeleteAllItems()
for index, student in enumerate(self.students_list):
self.list_ctrl_students.InsertItem(index, student[0])
self.list_ctrl_students.SetItem(index, 1, student[1])
self.list_ctrl_students.SetItem(index, 2, student[2])
def on_save_info(self, event):
with open("students_info.pkl", "wb") as file:
pickle.dump(self.students_list, file)
wx.MessageBox("学生信息已保存到文件!", "保存成功", wx.OK | wx.ICON_INFORMATION)
def on_load_info(self, event):
try:
with open("students_info.pkl", "rb") as file:
self.students_list = pickle.load(file)
self.update_student_list()
wx.MessageBox("从文件加载学生信息成功!", "加载成功", wx.OK | wx.ICON_INFORMATION)
except FileNotFoundError:
wx.MessageBox("找不到学生信息文件!", "加载失败", wx.OK | wx.ICON_ERROR)
class MyApp(wx.App):
def OnInit(self):
self.frame = StudentInfoFrame(None, title='学生信息管理系统')
self.frame.Show()
return True
if __name__ == '__main__':
app = MyApp()
app.MainLoop()