编写python,tkinter并连接数据库完成学生信息管理统计表,具备管理员的增删改查登录功能,和学生学号,姓名,性别,出生日期,籍贯,手机号的增删改查并退出的功能。
时间: 2023-10-08 11:12:49 浏览: 85
好的,这是一个比较复杂的项目,需要分步骤来完成。首先,我们需要安装并导入必要的库:
```python
import tkinter as tk
import sqlite3
```
然后,我们创建一个主窗口,并设置一些基本属性:
```python
root = tk.Tk()
root.title('学生信息管理系统')
root.geometry('600x400')
root.resizable(False, False)
```
接下来,我们需要创建一个数据库,并建立一个学生信息表:
```python
conn = sqlite3.connect('students.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
gender TEXT NOT NULL,
birthdate TEXT NOT NULL,
hometown TEXT NOT NULL,
phone_number TEXT NOT NULL
);
''')
conn.commit()
```
然后,我们需要创建一些用于显示和输入数据的控件,如标签、文本框、列表框和按钮:
```python
# 标签
tk.Label(root, text='学号').grid(row=0, column=0, padx=10, pady=5)
tk.Label(root, text='姓名').grid(row=0, column=1, padx=10, pady=5)
tk.Label(root, text='性别').grid(row=0, column=2, padx=10, pady=5)
tk.Label(root, text='出生日期').grid(row=0, column=3, padx=10, pady=5)
tk.Label(root, text='籍贯').grid(row=0, column=4, padx=10, pady=5)
tk.Label(root, text='手机号').grid(row=0, column=5, padx=10, pady=5)
# 文本框
student_id_entry = tk.Entry(root)
name_entry = tk.Entry(root)
gender_entry = tk.Entry(root)
birthdate_entry = tk.Entry(root)
hometown_entry = tk.Entry(root)
phone_number_entry = tk.Entry(root)
student_id_entry.grid(row=1, column=0, padx=10, pady=5)
name_entry.grid(row=1, column=1, padx=10, pady=5)
gender_entry.grid(row=1, column=2, padx=10, pady=5)
birthdate_entry.grid(row=1, column=3, padx=10, pady=5)
hometown_entry.grid(row=1, column=4, padx=10, pady=5)
phone_number_entry.grid(row=1, column=5, padx=10, pady=5)
# 列表框
student_listbox = tk.Listbox(root, height=10, width=60)
student_listbox.grid(row=2, column=0, columnspan=6, padx=10, pady=5)
# 按钮
add_button = tk.Button(root, text='增加', width=10, command=add_student)
delete_button = tk.Button(root, text='删除', width=10, command=delete_student)
update_button = tk.Button(root, text='修改', width=10, command=update_student)
search_button = tk.Button(root, text='查询', width=10, command=search_student)
exit_button = tk.Button(root, text='退出', width=10, command=root.quit)
add_button.grid(row=3, column=0, padx=10, pady=5)
delete_button.grid(row=3, column=1, padx=10, pady=5)
update_button.grid(row=3, column=2, padx=10, pady=5)
search_button.grid(row=3, column=3, padx=10, pady=5)
exit_button.grid(row=3, column=4, padx=10, pady=5)
```
接下来,我们需要实现一些功能函数,如增加学生信息、删除学生信息、修改学生信息、查询学生信息和退出程序:
```python
# 增加学生信息
def add_student():
student_id = student_id_entry.get()
name = name_entry.get()
gender = gender_entry.get()
birthdate = birthdate_entry.get()
hometown = hometown_entry.get()
phone_number = phone_number_entry.get()
if not student_id or not name or not gender or not birthdate or not hometown or not phone_number:
tk.messagebox.showerror('错误', '请填写完整信息!')
return
cursor.execute('INSERT INTO students (student_id, name, gender, birthdate, hometown, phone_number) VALUES (?, ?, ?, ?, ?, ?)', (student_id, name, gender, birthdate, hometown, phone_number))
conn.commit()
student_listbox.insert(tk.END, (student_id, name, gender, birthdate, hometown, phone_number))
student_id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
birthdate_entry.delete(0, tk.END)
hometown_entry.delete(0, tk.END)
phone_number_entry.delete(0, tk.END)
# 删除学生信息
def delete_student():
selected_student = student_listbox.curselection()
if not selected_student:
tk.messagebox.showerror('错误', '请选择要删除的学生!')
return
student_id = student_listbox.get(selected_student)[0]
cursor.execute('DELETE FROM students WHERE student_id = ?', (student_id,))
conn.commit()
student_listbox.delete(selected_student)
# 修改学生信息
def update_student():
selected_student = student_listbox.curselection()
if not selected_student:
tk.messagebox.showerror('错误', '请选择要修改的学生!')
return
student_id = student_listbox.get(selected_student)[0]
name = name_entry.get()
gender = gender_entry.get()
birthdate = birthdate_entry.get()
hometown = hometown_entry.get()
phone_number = phone_number_entry.get()
if not name or not gender or not birthdate or not hometown or not phone_number:
tk.messagebox.showerror('错误', '请填写完整信息!')
return
cursor.execute('UPDATE students SET name = ?, gender = ?, birthdate = ?, hometown = ?, phone_number = ? WHERE student_id = ?', (name, gender, birthdate, hometown, phone_number, student_id))
conn.commit()
student_listbox.delete(selected_student)
student_listbox.insert(selected_student, (student_id, name, gender, birthdate, hometown, phone_number))
student_id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
birthdate_entry.delete(0, tk.END)
hometown_entry.delete(0, tk.END)
phone_number_entry.delete(0, tk.END)
# 查询学生信息
def search_student():
student_id = student_id_entry.get()
name = name_entry.get()
gender = gender_entry.get()
birthdate = birthdate_entry.get()
hometown = hometown_entry.get()
phone_number = phone_number_entry.get()
query = 'SELECT * FROM students WHERE 1 = 1'
if student_id:
query += ' AND student_id = \'' + student_id + '\''
if name:
query += ' AND name = \'' + name + '\''
if gender:
query += ' AND gender = \'' + gender + '\''
if birthdate:
query += ' AND birthdate = \'' + birthdate + '\''
if hometown:
query += ' AND hometown = \'' + hometown + '\''
if phone_number:
query += ' AND phone_number = \'' + phone_number + '\''
cursor.execute(query)
students = cursor.fetchall()
student_listbox.delete(0, tk.END)
for student in students:
student_listbox.insert(tk.END, student)
# 退出程序
def exit_program():
conn.close()
root.destroy()
```
最后,我们将退出程序功能绑定到窗口的关闭事件上,并启动主循环:
```python
root.protocol('WM_DELETE_WINDOW', exit_program)
root.mainloop()
```
这样,一个简单的学生信息管理系统就完成了。
阅读全文