python学生信息管理系统tkinter代码
时间: 2023-07-22 13:37:38 浏览: 103
python+tkinter实现学生管理系统
5星 · 资源好评率100%
以下是一个简单的Python学生信息管理系统的Tkinter代码示例:
```
from tkinter import *
import tkinter.messagebox as MessageBox
import mysql.connector as mysql
# 连接到MySQL数据库
con = mysql.connect(host='localhost', user='root', password='', database='students')
cursor = con.cursor()
# 添加新学生信息
def add():
id = e_id.get()
name = e_name.get()
email = e_email.get()
if (id=="" or name=="" or email==""):
MessageBox.showinfo("Insert Status", "All fields are required")
else:
cursor.execute("insert into student values('" + id + "', '" + name + "', '" + email + "')")
cursor.execute("commit")
MessageBox.showinfo("Insert Status", "Inserted Successfully")
# 查找学生信息
def find():
id = e_id.get()
if (id==""):
MessageBox.showinfo("Fetch Status", "ID is mandatory to search")
else:
cursor.execute("select * from student where id='" + id + "'")
rows = cursor.fetchall()
if len(rows)!=0:
for row in rows:
e_name.delete(0, END)
e_name.insert(END, row[1])
e_email.delete(0, END)
e_email.insert(END, row[2])
else:
MessageBox.showinfo("Fetch Status", "No record found")
# 更新学生信息
def update():
id = e_id.get()
name = e_name.get()
email = e_email.get()
if (id=="" or name=="" or email==""):
MessageBox.showinfo("Update Status", "All fields are required")
else:
cursor.execute("update student set name='" + name + "', email='" + email + "' where id='" + id + "'")
cursor.execute("commit")
MessageBox.showinfo("Update Status", "Updated Successfully")
# 删除学生信息
def delete():
id = e_id.get()
if (id==""):
MessageBox.showinfo("Delete Status", "ID is mandatory to delete")
else:
cursor.execute("delete from student where id='" + id + "'")
cursor.execute("commit")
MessageBox.showinfo("Delete Status", "Deleted Successfully")
# 创建GUI窗口
root = Tk()
root.geometry("600x300")
root.title("Student Management System")
# 创建标签和文本框
label_id = Label(root, text="ID", font=("bold", 10))
label_id.place(x=20, y=30)
e_id = Entry()
e_id.place(x=100, y=30)
label_name = Label(root, text="Name", font=("bold", 10))
label_name.place(x=20, y=60)
e_name = Entry()
e_name.place(x=100, y=60)
label_email = Label(root, text="Email", font=("bold", 10))
label_email.place(x=20, y=90)
e_email = Entry()
e_email.place(x=100, y=90)
# 创建按钮
btn_add = Button(root, text="Add", font=("italic", 10), bg="white", command=add)
btn_add.place(x=20, y=150)
btn_find = Button(root, text="Find", font=("italic", 10), bg="white", command=find)
btn_find.place(x=80, y=150)
btn_update = Button(root, text="Update", font=("italic", 10), bg="white", command=update)
btn_update.place(x=130, y=150)
btn_delete = Button(root, text="Delete", font=("italic", 10), bg="white", command=delete)
btn_delete.place(x=200, y=150)
# 运行程序
root.mainloop()
```
请注意,在使用此代码之前,您需要在本地计算机上安装MySQL数据库,并创建名为“students”的数据库和名为“student”的表。此代码仅提供基本的学生信息管理功能,您可以根据需要进行修改和扩展。
阅读全文