用python代码写一个学生管理系统,连接sql sever数据库,并实现界面化以及实现以下功能:登录界面、显示学生的信息、对学生的信息实现增删改等
时间: 2024-03-10 10:50:48 浏览: 65
用python写的学生管理系统
好的,下面是一个使用SQL Server数据库的学生管理系统的Python代码,使用pyodbc模块连接数据库,并使用Tkinter实现界面化:
```python
import pyodbc
from tkinter import *
# 连接数据库
server = 'localhost'
database = 'students'
username = 'sa'
password = 'password'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = conn.cursor()
# 创建学生表
cursor.execute('''CREATE TABLE IF NOT EXISTS students
(id INT PRIMARY KEY IDENTITY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL)''')
conn.commit()
# 登录界面
def login():
username = username_entry.get()
password = password_entry.get()
if username == 'admin' and password == 'password':
show_students()
else:
messagebox.showerror("错误", "用户名或密码错误!")
# 添加学生信息
def add_student():
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
cursor.execute("INSERT INTO students (name, age, gender) VALUES (?, ?, ?)", (name, age, gender))
conn.commit()
show_students()
# 显示学生信息
def show_students():
student_list.delete(0, END)
cursor.execute("SELECT * FROM students")
students = cursor.fetchall()
for student in students:
student_list.insert(END, student)
# 删除学生信息
def delete_student():
selected_student = student_list.get(student_list.curselection())
cursor.execute("DELETE FROM students WHERE id=?", (selected_student[0],))
conn.commit()
show_students()
# 修改学生信息
def update_student():
selected_student = student_list.get(student_list.curselection())
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
cursor.execute("UPDATE students SET name=?, age=?, gender=? WHERE id=?", (name, age, gender, selected_student[0]))
conn.commit()
show_students()
# 创建界面
root = Tk()
root.title("学生管理系统")
username_label = Label(root, text="用户名")
username_label.grid(row=0, column=0)
password_label = Label(root, text="密码")
password_label.grid(row=1, column=0)
username_entry = Entry(root)
username_entry.grid(row=0, column=1)
password_entry = Entry(root, show="*")
password_entry.grid(row=1, column=1)
login_button = Button(root, text="登录", command=login)
login_button.grid(row=2, column=0, columnspan=2)
name_label = Label(root, text="姓名")
name_label.grid(row=3, column=0)
age_label = Label(root, text="年龄")
age_label.grid(row=4, column=0)
gender_label = Label(root, text="性别")
gender_label.grid(row=5, column=0)
name_entry = Entry(root)
name_entry.grid(row=3, column=1)
age_entry = Entry(root)
age_entry.grid(row=4, column=1)
gender_entry = Entry(root)
gender_entry.grid(row=5, column=1)
add_button = Button(root, text="添加", command=add_student)
add_button.grid(row=3, column=2)
delete_button = Button(root, text="删除", command=delete_student)
delete_button.grid(row=4, column=2)
update_button = Button(root, text="修改", command=update_student)
update_button.grid(row=5, column=2)
student_list = Listbox(root)
student_list.grid(row=6, column=0, columnspan=3)
root.mainloop()
# 关闭数据库连接
cursor.close()
conn.close()
```
在运行代码之前,请确保已经安装了pyodbc模块。运行后,程序将会创建一个名为“students”的SQL Server数据库,并创建一个名为“students”的表来存储学生信息。在登录界面中输入正确的用户名和密码后,您可以通过添加、删除和修改学生信息来测试该程序。
阅读全文