请大佬帮我看一下这段代码的问题(有详细说明),大一新生被迫做课程设计
以下是执行操作之后的结果:
这个是Navicat 16 for MySQL里面的表:
这个是代码
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import tkinter
import pymysql
from tkinter import ttk
# 打开数据库连接
def Sql_link():
# 生成数据库
db = pymysql.connect(host='localhost', user='root', password='ybw200662', database='student')
# 使用 cursor() 方法创建一个游标对象
cursor = db.cursor()
return db, cursor
# 管理员注册
def create_manager():
# 创建窗口
create_manager_root = Tk()
create_manager_root.title("管理员用户创建")
create_manager_root.config(width=600)
create_manager_root.config(height=600)
# 定义关联变量
manager_name = StringVar(create_manager_root, value='')
manager_code = StringVar(create_manager_root, value='')
# 提示性标签
labelmanager_name = Label(create_manager_root, text="管理员ID注册", font=("微软雅黑 -20"))
labelmanager_code = Label(create_manager_root, text="管理员密码设置", font=("微软雅黑 -20"))
# 设定标签位置
labelmanager_name.place(x=200, y=100, height=40, width=200)
labelmanager_code.place(x=200, y=200, height=40, width=200)
# 定义录入信息文本框,以关联变量形式存储
entrymanager_name = Entry((create_manager_root), textvariable=manager_name)
entrymanager_name.place(x=200, y=150, height=40, width=200)
entrymanager_code = Entry((create_manager_root), textvariable=manager_code)
entrymanager_code.place(x=200, y=250, height=40, width=200)
# 录入信息回调函数
def Button_Ok():
# 设置标志位判断是否存在ID重复
flag = 0
# 连接数据库
db, cur = Sql_link()
# 将得到的StringVar对象传值回来
data_manager_name = str(entrymanager_name.get())
data_manager_code = str(entrymanager_code.get())
# 判断是否ID重复
search = cur.execute("SELECT * FROM manager WHERE name = " + data_manager_name + ';')
if (search > 0):
flag = 1
else:
flag = 0
if (flag == 0):
try:
# 写入数据
sql1 = "INSERT INTO manager(name,code)"
sql1 += "VALUES('%s','%s')" % (data_manager_name, data_manager_code)
cur.execute(sql1)
db.commit()
messagebox.showinfo(title="恭喜", message="注册成功!!!")
create_manager_root.destroy()
except:
messagebox.showerror(message="注册失败!!!")
else:
messagebox.showerror("该用户名已注册!!!")
# 确认以及退出按钮
Ok_Button = Button(create_manager_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(create_manager_root, text="退出", font=("微软雅黑 -20"), command=create_manager_root.destroy)
Ok_Button.place(x=75, y=350, height=40, width=200)
Exit_Button.place(x=325, y=350, height=40, width=200)
# 管理员登录
def load_manager():
# 创建窗口
load_manager_root = Tk()
load_manager_root.title("管理员用户创建")
load_manager_root.config(width=600)
load_manager_root.config(height=600)
# 定义关联变量
manager_name = StringVar(load_manager_root, value='')
manager_code = StringVar(load_manager_root, value='')
# 提示性标签
labelmanager_name = Label(load_manager_root, text="管理员ID", font=("微软雅黑 -20"))
labelmanager_code = Label(load_manager_root, text="管理员密码", font=("微软雅黑 -20"))
# 设定标签位置
labelmanager_name.place(x=200, y=100, height=40, width=200)
labelmanager_code.place(x=200, y=200, height=40, width=200)
# 定义录入信息文本框,以关联变量形式存储
entrymanager_name = Entry((load_manager_root), textvariable=manager_name)
entrymanager_name.place(x=200, y=150, height=40, width=200)
entrymanager_code = Entry((load_manager_root), textvariable=manager_code)
entrymanager_code.place(x=200, y=250, height=40, width=200)
def Button_Ok():
# 连接数据库
db, cur = Sql_link()
# 将得到的StringVar对象传值回来
data_manager_name = str(entrymanager_name.get())
data_manager_code = str(entrymanager_code.get())
# 判断ID与密码是否能够匹配上
sql = "SELECT * FROM manager WHERE name=%s AND code='%s'" % (data_manager_name, data_manager_code)
search = cur.execute(sql)
if (search > 0):
messagebox.showinfo(title="恭喜", message="登录成功!!!")
show_student()
load_manager_root.destroy()
else:
messagebox.showerror(message="该账号不存在!")
Ok_Button = Button(load_manager_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(load_manager_root, text="退出", font=("微软雅黑 -20"), command=load_manager_root.destroy)
Ok_Button.place(x=75, y=350, height=40, width=200)
Exit_Button.place(x=325, y=350, height=40, width=200)
# 管理员信息修改
def change_manager():
# 创建窗口
change_manager_root = Tk()
change_manager_root.title("管理员信息修改")
change_manager_root.config(width=600)
change_manager_root.config(height=600)
# 删除信息
def delete_manager():
# 创建窗口
delete_manager_root = Tk()
delete_manager_root.title("管理员信息删除")
delete_manager_root.config(width=600)
delete_manager_root.config(height=600)
manager_name = StringVar(delete_manager_root, value='')
# 提示性标签
labelmanager_name = Label(delete_manager_root, text="要删除的管理员ID", font=("微软雅黑 -20"))
entrymanager_name = Entry(delete_manager_root, textvariable=manager_name)
labelmanager_name.place(x=200, y=100, height=40, width=200)
entrymanager_name.place(x=200, y=200, height=40, width=200)
def Button_Ok():
db, cur = Sql_link()
manager_name = eval(entrymanager_name.get())
# 查找对应的ID号
sql = "SELECT * FROM manager WHERE name = %s" % (manager_name)
search = cur.execute(sql)
if (search > 0):
messagebox.showinfo(message="删除成功!")
# 删除目标行
sql1 = "DELETE FROM manager WHERE name = %s" % (manager_name)
cur.execute(sql1)
db.commit()
else:
messagebox.showerror(message="该用户不存在!")
Ok_Button = Button(delete_manager_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(delete_manager_root, text="退出", font=("微软雅黑 -20"),
command=delete_manager_root.destroy)
Ok_Button.place(x=75, y=350, height=40, width=200)
Exit_Button.place(x=325, y=350, height=40, width=200)
# 修改密码
def change_code():
# 创建窗口
change_code_root = Tk()
change_code_root.title("管理员信息删除")
change_code_root.config(width=600)
change_code_root.config(height=600)
manager_name = StringVar(change_code_root, value='')
old_manager_code = StringVar(change_code_root, value='')
# 提示性标签
labelmanager_name = Label(change_code_root, text="要修改的管理员ID", font=("微软雅黑 -20"))
entrymanager_name = Entry(change_code_root, textvariable=manager_name)
labelold_manager_code = Label(change_code_root, text="请输入原密码", font=("微软雅黑 -20"))
entryold_manager_code = Entry(change_code_root, textvariable=old_manager_code)
labelmanager_name.place(x=200, y=50, height=40, width=200)
entrymanager_name.place(x=200, y=150, height=40, width=200)
labelold_manager_code.place(x=200, y=250, height=40, width=200)
entryold_manager_code.place(x=200, y=350, height=40, width=200)
def Button_Ok():
db, cur = Sql_link()
manager_name = eval(entrymanager_name.get())
# 匹配对应的ID
sql = "SELECT * FROM manager WHERE name = %s" % (manager_name)
search = cur.execute(sql)
if (search > 0):
# 创建新窗口
input_code_root = Tk()
input_code_root.title("管理员密码修改")
input_code_root.config(width=600)
input_code_root.config(height=600)
# 设置关联变量
new_manager_code = StringVar(input_code_root, value='')
labelnew_manager_code = Label(input_code_root, text="新的密码", font=("微软雅黑 -20"))
entrynew_manager_code = Entry(input_code_root, textvariable=new_manager_code)
# 设置提示性标签
labelnew_manager_code.place(x=200, y=200, height=40, width=200)
entrynew_manager_code.place(x=200, y=300, height=40, width=200)
def Button_Ok():
try:
new_manager_code = entrynew_manager_code.get()
messagebox.showinfo(message="修改成功!")
# 利用UPDATE来修改密码
sql1 = "UPDATE manager SET code = %s WHERE name = %s" % (new_manager_code, manager_name)
cur.execute(sql1)
db.commit()
except:
messagebox.showerror(message="修改失败!")
Ok_Button = Button(input_code_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(input_code_root, text="退出", font=("微软雅黑 -20"),
command=input_code_root.destroy)
Ok_Button.place(x=75, y=350, height=40, width=200)
Exit_Button.place(x=325, y=350, height=40, width=200)
else:
messagebox.showerror(message="该用户不存在!")
Ok_Button = Button(change_code_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(change_code_root, text="退出", font=("微软雅黑 -20"), command=change_code_root.destroy)
Ok_Button.place(x=75, y=450, height=40, width=200)
Exit_Button.place(x=325, y=450, height=40, width=200)
# 设置按钮
button_stu1 = Button(change_manager_root, text="删除账号", font=("微软雅黑 -20"), command=delete_manager)
button_stu2 = Button(change_manager_root, text="修改密码", font=("微软雅黑 -20"), command=change_code)
button_stu1.place(x=200, y=100, height=40, width=200)
button_stu2.place(x=200, y=200, height=40, width=200)
Exit_Button = Button(change_manager_root, text="退出", font=("微软雅黑 -20"), command=change_manager_root.destroy)
Exit_Button.place(x=200, y=300, height=40, width=200)
# 展示学生信息
def show_student():
# 创建窗口
show_student_root = Tk()
show_student_root.title("录入学生信息")
show_student_root.config(width=600)
show_student_root.config(height=600)
# 输入信息
def input_grade():
input_grade_root = Tk()
input_grade_root.title("学生成绩录入")
input_grade_root.config(width=600)
input_grade_root.config(height=900)
# 定义关联变量
student_id = StringVar(input_grade_root, value='')
python_grade = StringVar(input_grade_root, value='')
# 提示性标签
labelstudent_id = Label(input_grade_root, text="学生ID", font=("微软雅黑 -20"))
labelpython_grade = Label(input_grade_root, text="Python成绩", font=("微软雅黑 -20"))
# 设定标签位置
labelstudent_id.place(x=200, y=50, height=40, width=200)
labelpython_grade.place(x=200, y=250, height=40, width=200)
# 定义录入信息文本框,以关联变量形式存储
entrystudent_id = Entry(input_grade_root, textvariable=student_id)
entrystudent_id.place(x=200, y=100, height=40, width=200)
entrypython_grade = Entry(input_grade_root, textvariable=python_grade)
entrypython_grade.place(x=200, y=400, height=40, width=200)
# 确认以及退出按钮
def Button_Ok():
# 注意数据类型转换
db, cur = Sql_link()
student_id = eval(entrystudent_id.get())
python_grade = eval(entrypython_grade.get())
Ok_Button = Button(input_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(input_grade_root, text="退出", font=("微软雅黑 -20"), command=input_grade_root.destroy)
Ok_Button.place(x=75, y=700, height=40, width=200)
Exit_Button.place(x=325, y=700, height=40, width=200)
# 查询信息
def search_grade():
search_grade_root = Tk()
search_grade_root.title("学生信息查询")
search_grade_root.config(width=600)
search_grade_root.config(height=600)
# 定义关联变量
student_id = StringVar(search_grade_root, value='')
# 提示性标签
labelstudent_id = Label(search_grade_root, text="要查询学生的ID", font=("微软雅黑 -20"))
# 设定标签位置
labelstudent_id.place(x=200, y=150, height=40, width=200)
# 定义录入信息文本框,以关联变量形式存储
entrystudent_id = Entry(search_grade_root, textvariable=student_id)
entrystudent_id.place(x=200, y=250, height=40, width=200)
def Button_Ok():
global python_grade1
show_student_message_root = Tk()
show_student_message_root.title("学生信息查询")
show_student_message_root.config(width=600)
show_student_message_root.config(height=700)
# 创建关联变量
python_grade = StringVar(show_student_message_root, value='')
db, cur = Sql_link()
student_id = eval(entrystudent_id.get())
# 查找ID对应的学生
sql = "SELECT * FROM student WHERE ID = %d" % (student_id)
num = cur.execute(sql)
if num > 0:
cursor = cur.fetchall()
for row in cursor:
if (student_id == row[0]):
python_grade1 = row[1]
python_grade.set(python_grade1)
# 提示性标签
labelpython_grade = Label(show_student_message_root, text="Python成绩", font=("微软雅黑 -20"))
# 设定标签位置
labelpython_grade.place(x=200, y=300, height=40, width=200)
# 配置文本框数据
entrypython_grade = Entry((show_student_message_root), textvariable=python_grade)
entrypython_grade.place(x=200, y=350, height=40, width=200)
Exit_Button = Button(show_student_message_root, text="退出", font=("微软雅黑 -20"),
command=show_student_message_root.destroy)
Exit_Button.place(x=200, y=630, height=40, width=200)
else:
messagebox.showerror(message="此用户不存在!")
Ok_Button = Button(search_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
Exit_Button = Button(search_grade_root, text="退出", font=("微软雅黑 -20"), command=search_grade_root.destroy)
Ok_Button.place(x=75, y=350, height=40, width=200)
Exit_Button.place(x=325, y=350, height=40, width=200)
# 设置按钮
button_stu1 = Button(show_student_root, text="录入成绩信息", font=("微软雅黑 -20"), command=input_grade)
button_stu2 = Button(show_student_root, text="查询成绩信息", font=("微软雅黑 -20"), command=search_grade)
button_stu1.place(x=200, y=30, height=40, width=200)
button_stu2.place(x=200, y=130, height=40, width=200)
Exit_Button = Button(show_student_root, text="退出", font=("微软雅黑 -20"), command=show_student_root.destroy)
Exit_Button.place(x=200, y=530, height=40, width=200)
# 设置登录界面
root = Tk()
# 禁止最大化按钮(只显示最小化按钮和关闭按钮)
root.resizable(False, False)
root.minsize(600, 600) # 最小尺寸
root.maxsize(600, 600) # 最大尺寸
root.title("学生信息管理系统")
# 设置长宽
root.config(width=600)
root.config(height=600)
main_button1 = Button(root, text="管理员用户创建", font=("微软雅黑 -20"), command=create_manager)
main_button2 = Button(root, text="管理员密码登录", font=("微软雅黑 -20"), command=load_manager)
main_button3 = Button(root, text="管理员信息修改", font=("微软雅黑 -20"), command=change_manager)
main_button1.place(x=200, y=100, height=40, width=200)
main_button2.place(x=200, y=200, height=40, width=200)
main_button3.place(x=200, y=300, height=40, width=200)
Exit_Button = Button(root, text="退出", font=("微软雅黑 -20"), command=root.destroy)
Exit_Button.place(x=200, y=400, height=40, width=200)
root.mainloop()
球球大佬帮忙,感谢!
推荐文章: