基于学生实习管理系统的航天实习请假功能实现
小明:嘿,小李,最近我们学校的‘学生实习管理系统’要加入一个新的功能,是关于航天实习期间的请假申请,你有什么想法吗?
小李:嗯,我觉得可以先从数据库设计入手。我们需要一个表来存储请假信息,比如学生ID、实习单位、请假开始时间、结束时间以及请假原因。
小明:对,那我们可以用SQLite数据库,它简单易用。首先创建一个名为'leave_application'的表。
import sqlite3
conn = sqlite3.connect('student_internship.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE leave_application (
id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id TEXT NOT NULL,
company_name TEXT NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
reason TEXT NOT NULL
);
''')
conn.commit()
conn.close()
小李:然后,我们需要编写一个简单的界面让用户提交请假申请。我建议用Tkinter库快速搭建一个GUI。
import tkinter as tk
from tkinter import messagebox
def submit_leave():
student_id = entry_student_id.get()
company_name = entry_company_name.get()
start_time = entry_start_time.get()
end_time = entry_end_time.get()
reason = entry_reason.get()
if not (student_id and company_name and start_time and end_time and reason):
messagebox.showerror("错误", "所有字段都必须填写")
return
try:
conn = sqlite3.connect('student_internship.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO leave_application (student_id, company_name, start_time, end_time, reason)
VALUES (?, ?, ?, ?, ?);
''', (student_id, company_name, start_time, end_time, reason))
conn.commit()
messagebox.showinfo("成功", "请假申请已提交")
except Exception as e:
messagebox.showerror("错误", str(e))
finally:
conn.close()
root = tk.Tk()
root.title("航天实习请假系统")
label_student_id = tk.Label(root, text="学生ID:")
label_student_id.grid(row=0, column=0)
entry_student_id = tk.Entry(root)
entry_student_id.grid(row=0, column=1)
label_company_name = tk.Label(root, text="实习单位:")
label_company_name.grid(row=1, column=0)
entry_company_name = tk.Entry(root)
entry_company_name.grid(row=1, column=1)
label_start_time = tk.Label(root, text="开始时间:")
label_start_time.grid(row=2, column=0)
entry_start_time = tk.Entry(root)
entry_start_time.grid(row=2, column=1)
label_end_time = tk.Label(root, text="结束时间:")
label_end_time.grid(row=3, column=0)
entry_end_time = tk.Entry(root)
entry_end_time.grid(row=3, column=1)
label_reason = tk.Label(root, text="请假原因:")
label_reason.grid(row=4, column=0)
entry_reason = tk.Entry(root)
entry_reason.grid(row=4, column=1)
btn_submit = tk.Button(root, text="提交", command=submit_leave)
btn_submit.grid(row=5, column=1)
root.mainloop()
小明:这样用户就可以通过这个界面提交他们的请假信息了。接下来我们需要一个管理员界面来查看这些申请。
def view_leaves():
try:
conn = sqlite3.connect('student_internship.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM leave_application;")
rows = cursor.fetchall()
for row in rows:
print(f"ID: {row[0]}, 学生ID: {row[1]}, 单位: {row[2]}, 开始时间: {row[3]}, 结束时间: {row[4]}, 原因: {row[5]}")
except Exception as e:
print(f"Error: {e}")
finally:
conn.close()
view_leaves()
小李:现在我们有了基本的功能,管理员可以通过命令行查看所有的请假记录。
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!