GUI py带有登录界面的学生信息管理系统代码
时间: 2023-07-03 11:19:27 浏览: 97
以下是一个基于Python Tkinter库实现的带有登录界面的学生信息管理系统代码,供你参考:
```python
import tkinter as tk
from tkinter import ttk
import pymysql
# 创建MySQL数据库连接
conn = pymysql.connect(host='localhost', user='root', password='password', database='students')
cursor = conn.cursor()
# 创建主窗口
root = tk.Tk()
root.title("学生信息管理系统")
root.geometry("500x300")
root.resizable(width=False, height=False)
# 创建登录窗口
def login_window():
# 销毁主窗口
root.destroy()
# 创建登录窗口
login = tk.Tk()
login.title("登录")
login.geometry("300x200")
login.resizable(width=False, height=False)
# 创建用户名和密码标签
username_label = tk.Label(login, text="用户名:")
username_label.place(x=30, y=50)
password_label = tk.Label(login, text="密码:")
password_label.place(x=30, y=90)
# 创建用户名和密码输入框
username_entry = tk.Entry(login)
username_entry.place(x=80, y=50)
password_entry = tk.Entry(login, show="*")
password_entry.place(x=80, y=90)
# 创建登录按钮
def login_func():
username = username_entry.get()
password = password_entry.get()
query = "SELECT * FROM users WHERE username=%s AND password=%s"
cursor.execute(query, (username, password))
result = cursor.fetchone()
if result:
login.destroy()
main_window()
else:
tk.messagebox.showerror("错误", "用户名或密码错误")
login_button = tk.Button(login, text="登录", command=login_func)
login_button.place(x=130, y=130)
login.mainloop()
# 创建主窗口
def main_window():
# 创建菜单栏
menubar = tk.Menu(root)
root.config(menu=menubar)
# 创建菜单
file_menu = tk.Menu(menubar, tearoff=0)
edit_menu = tk.Menu(menubar, tearoff=0)
help_menu = tk.Menu(menubar, tearoff=0)
# 添加菜单
menubar.add_cascade(label="文件", menu=file_menu)
menubar.add_cascade(label="编辑", menu=edit_menu)
menubar.add_cascade(label="帮助", menu=help_menu)
# 创建标签
title_label = tk.Label(root, text="学生信息管理系统", font=('Arial', 20))
title_label.place(x=150, y=10)
# 创建学生信息表格
student_tree = ttk.Treeview(root, columns=("编号", "姓名", "年龄", "性别", "专业"), show="headings")
student_tree.column("编号", width=50)
student_tree.column("姓名", width=100)
student_tree.column("年龄", width=50)
student_tree.column("性别", width=50)
student_tree.column("专业", width=150)
student_tree.heading("编号", text="编号")
student_tree.heading("姓名", text="姓名")
student_tree.heading("年龄", text="年龄")
student_tree.heading("性别", text="性别")
student_tree.heading("专业", text="专业")
# 添加学生信息数据
query = "SELECT * FROM students"
cursor.execute(query)
result = cursor.fetchall()
for row in result:
student_tree.insert("", "end", values=row)
student_tree.place(x=25, y=50)
# 创建添加学生信息界面
def add_student():
add_window = tk.Tk()
add_window.title("添加学生信息")
add_window.geometry("200x150")
add_window.resizable(width=False, height=False)
# 创建标签和输入框
name_label = tk.Label(add_window, text="姓名:")
name_label.place(x=10, y=20)
name_entry = tk.Entry(add_window)
name_entry.place(x=60, y=20)
age_label = tk.Label(add_window, text="年龄:")
age_label.place(x=10, y=50)
age_entry = tk.Entry(add_window)
age_entry.place(x=60, y=50)
gender_label = tk.Label(add_window, text="性别:")
gender_label.place(x=10, y=80)
gender_entry = tk.Entry(add_window)
gender_entry.place(x=60, y=80)
major_label = tk.Label(add_window, text="专业:")
major_label.place(x=10, y=110)
major_entry = tk.Entry(add_window)
major_entry.place(x=60, y=110)
# 创建添加学生信息按钮
def add_student_func():
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
major = major_entry.get()
query = "INSERT INTO students (name, age, gender, major) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (name, age, gender, major))
conn.commit()
student_tree.insert("", "end", values=(cursor.lastrowid, name, age, gender, major))
add_window.destroy()
add_button = tk.Button(add_window, text="添加", command=add_student_func)
add_button.place(x=100, y=140)
add_window.mainloop()
# 创建添加学生信息按钮
add_button = tk.Button(root, text="添加学生信息", command=add_student)
add_button.place(x=25, y=250)
root.mainloop()
# 运行程序
login_window()
```
这段代码实现了一个基本的学生信息管理系统,包括登录功能和学生信息的添加和展示功能。需要在MySQL数据库中创建名为`students`的数据库,并创建名为`students`和`users`的数据表。其中`students`表用于存储学生信息,包括`id`、`name`、`age`、`gender`和`major`字段,`users`表用于存储用户信息,包括`id`、`username`和`password`字段。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)