设计一个添加和删除学生信息的窗口程序,并通过表格形式显示已有学生信息,学生信息包括学号,姓名,出生日期,出生地和班级
时间: 2024-03-17 07:43:06 浏览: 63
学生信息管理系统,实现学生信息包括:学号、姓名、性别、年龄、班级等信息。
5星 · 资源好评率100%
好的,我可以为您提供一个简单的Python程序来实现这个功能。程序需要使用tkinter模块来创建窗口和表格,并使用sqlite3模块来保存学生信息。
首先,安装必要的模块:
```python
pip install tkinter
pip install sqlite3
```
然后,创建一个名为student_info.db的SQLite数据库,其中包含一个名为students的表格。表格应包含以下列:id(自动递增),学号,姓名,出生日期,出生地和班级。
接下来,编写程序:
```python
import tkinter as tk
import sqlite3
from tkinter import ttk
from tkinter import messagebox as mb
class Student:
def __init__(self, id, student_id, name, birthdate, birthplace, class_name):
self.id = id
self.student_id = student_id
self.name = name
self.birthdate = birthdate
self.birthplace = birthplace
self.class_name = class_name
class StudentApp:
def __init__(self, root):
self.root = root
self.root.title("学生信息管理系统")
self.create_widgets()
self.create_table()
def create_widgets(self):
frame1 = tk.Frame(self.root)
frame1.pack(padx=10, pady=10)
tk.Label(frame1, text="学号:").grid(row=0, column=0, padx=5, pady=5)
self.student_id_entry = tk.Entry(frame1)
self.student_id_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(frame1, text="姓名:").grid(row=1, column=0, padx=5, pady=5)
self.name_entry = tk.Entry(frame1)
self.name_entry.grid(row=1, column=1, padx=5, pady=5)
tk.Label(frame1, text="出生日期:").grid(row=2, column=0, padx=5, pady=5)
self.birthdate_entry = tk.Entry(frame1)
self.birthdate_entry.grid(row=2, column=1, padx=5, pady=5)
tk.Label(frame1, text="出生地:").grid(row=3, column=0, padx=5, pady=5)
self.birthplace_entry = tk.Entry(frame1)
self.birthplace_entry.grid(row=3, column=1, padx=5, pady=5)
tk.Label(frame1, text="班级:").grid(row=4, column=0, padx=5, pady=5)
self.class_name_entry = tk.Entry(frame1)
self.class_name_entry.grid(row=4, column=1, padx=5, pady=5)
frame2 = tk.Frame(self.root)
frame2.pack(padx=10, pady=10)
self.add_button = tk.Button(frame2, text="添加", command=self.add_student)
self.add_button.pack(side=tk.LEFT, padx=5)
self.delete_button = tk.Button(frame2, text="删除", command=self.delete_student)
self.delete_button.pack(side=tk.LEFT, padx=5)
def create_table(self):
self.tree = ttk.Treeview(self.root, columns=("id", "student_id", "name", "birthdate", "birthplace", "class_name"))
self.tree.heading("#0", text="序号")
self.tree.heading("id", text="ID")
self.tree.heading("student_id", text="学号")
self.tree.heading("name", text="姓名")
self.tree.heading("birthdate", text="出生日期")
self.tree.heading("birthplace", text="出生地")
self.tree.heading("class_name", text="班级")
self.tree.pack(padx=10, pady=10)
self.update_table()
def update_table(self):
self.tree.delete(*self.tree.get_children())
conn = sqlite3.connect("student_info.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
for row in rows:
student = Student(row[0], row[1], row[2], row[3], row[4], row[5])
self.tree.insert("", "end", text=student.id, values=(student.id, student.student_id, student.name, student.birthdate, student.birthplace, student.class_name))
cursor.close()
conn.close()
def add_student(self):
student_id = self.student_id_entry.get()
name = self.name_entry.get()
birthdate = self.birthdate_entry.get()
birthplace = self.birthplace_entry.get()
class_name = self.class_name_entry.get()
if student_id == "" or name == "" or birthdate == "" or birthplace == "" or class_name == "":
mb.showerror("错误", "所有字段都必须填写")
return
conn = sqlite3.connect("student_info.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO students (student_id, name, birthdate, birthplace, class_name) VALUES (?, ?, ?, ?, ?)", (student_id, name, birthdate, birthplace, class_name))
conn.commit()
cursor.close()
conn.close()
self.update_table()
self.student_id_entry.delete(0, tk.END)
self.name_entry.delete(0, tk.END)
self.birthdate_entry.delete(0, tk.END)
self.birthplace_entry.delete(0, tk.END)
self.class_name_entry.delete(0, tk.END)
def delete_student(self):
selected_items = self.tree.selection()
if len(selected_items) == 0:
mb.showerror("错误", "请选择要删除的学生")
return
result = mb.askyesno("确认", "确定要删除所选学生吗?")
if result == tk.NO:
return
conn = sqlite3.connect("student_info.db")
cursor = conn.cursor()
for item in selected_items:
cursor.execute("DELETE FROM students WHERE id=?", (self.tree.item(item, "values")[0],))
conn.commit()
cursor.close()
conn.close()
self.update_table()
root = tk.Tk()
app = StudentApp(root)
root.mainloop()
```
这个程序将创建一个窗口,其中包含一些文本输入框和按钮。在文本框中输入学生信息并单击“添加”按钮,将把信息添加到数据库中,并在表格中显示。选中行并单击“删除”按钮将从数据库中删除该行。
注意:为了简化代码,本例中省略了一些错误检查。在实际应用中,您应该检查学生ID是否唯一,日期格式是否正确等。
阅读全文