5.16
"""************************ 删除的部分 ********************************************"""
#删除学生信息
def delete_stu():
root2=Tk()
root2.title("删除学生信息")
root2.config(width=600)
root2.config(height=600)
#添加窗口背景图片
#创建标签
label=Label(root2,text="学号:",font=("微软雅黑 -20"))
#label.grid(row=1,sticky=E)
label.place(x=130,y=50,height=40,width=80)
entryId=Entry(root2)
entryId.place(x=190,y=50,height=40,width=200)
def delete():
conn = sqlite3.connect('student1.db')
stu_id = eval(entryId.get())#学号输入
conn.execute("DELETE from StudentTable where StuId = '%s';"%stu_id)
conn.commit()
messagebox.showinfo(title='恭喜',message='删除成功!')
root2.destroy()
#删除键
buttondelete=Button(root2,text="删除",font=("微软雅黑 -20"),command=delete)
buttondelete.place(x=170,y=160,height=40,width=200)
#退出键
buttondel=Button(root2,text="退出",font=("微软雅黑 -20"),command=root2.destroy)
buttondel.place(x=170,y=210,height=40,width=200)
root2.mainloop()
#删除完成
"""******************************************************************************************"""
"""************************ 查询的部分 ********************************************"""
#查询学生信息
def sel_stu():
root3=Tk()
root3.title("查询学生信息")
root3.config(width=600)
root3.config(height=600)
#创建关联变量
sId=StringVar(root3,value='')
#创建文本组件框\标签组件
label=Label(root3,text="学号",font=("微软雅黑 -20"))
label.place(x=130,y=30,height=40,width=80)
selId=Entry((root3),textvariable=sId)
selId.place(x=190,y=30,height=40,width=200)
def select():
#创建关联字符变量
varName=StringVar(root3,value='')
varId=StringVar(root3,value='')
varClass=StringVar(root3,value='')
varAge=StringVar(root3,value='')
conn = sqlite3.connect('student1.db')
stu_id = eval(selId.get())#学号输入
cursor = conn.execute("SELECT * from StudentTable where StuId = '%d';"%stu_id)
conn.commit()
for row in cursor:
if stu_id == row[1]:
stu_name=row[2]
stu_class=row[3]
stu_age=row[4]
#创建标签组件
label=Label(root3,text="姓名:",font=("微软雅黑 -20"))
#label.grid(row=0,sticky=E)
label.place(x=130,y=160,height=40,width=80)
label=Label(root3,text="学号:",font=("微软雅黑 -20"))
#label.grid(row=1,sticky=E)
label.place(x=130,y=210,height=40,width=80)
label=Label(root3,text="班级:",font=("微软雅黑 -20"))
#label.grid(row=2,sticky=E)
label.place(x=130,y=260,height=40,width=80)
label=Label(root3,text="年龄:",font=("微软雅黑 -20"))
#label.grid(row=3,sticky=E)
label.place(x=130,y=310,height=40,width=80)
#创建文本框组件,同时设置关联的变量
# 姓名entryName
# 学号entryId
# 班级entryClass
# 年龄entryAge
entryName=Entry((root3),textvariable=varName)
#entryName.grid(row=0,column=1,sticky=W)
entryName.place(x=190,y=160,height=40,width=200)
entryId=Entry((root3),textvariable=varId)
#entryId.grid(row=1,column=1,sticky=W)
entryId.place(x=190,y=210,height=40,width=200)
entryClass=Entry((root3),textvariable=varClass)
#entryClass.grid(row=2,column=1,sticky=W)
entryClass.place(x=190,y=260,height=40,width=200)
entryAge=Entry((root3),textvariable=varAge)
#entryAge.grid(row=3,column=1,sticky=W)
entryAge.place(x=190,y=310,height=40,width=200)
varName.set(stu_name)
varId.set(stu_id)
varClass.set(stu_class)
varAge.set(stu_age)
#查询键
buttonselect=Button(root3,text="查询",font=("微软雅黑 -20"),command=select)
buttonselect.place(x=230,y=80,height=40,width=100)
#取消键
def cancel():
sId.set('')
buttoncancel=Button(root3,text="取消",font="微软雅黑 -20",command=cancel)
buttoncancel.place(x=100,y=80,height=40,width=100)
#退出键
buttondel=Button(root3,text="退出",font="微软雅黑 -20",command=root3.destroy)
buttondel.place(x=350,y=80,height=40,width=100)
root3.mainloop()
#查询完成
"""******************************************************************************************"""
"""************************ 修改的部分 ********************************************"""
#修改学生信息
def change_stu():
root4=Tk()
root4.title("修改学生信息")
root4.config(width=600)
root4.config(height=600)
#创建关联变量
sId=StringVar(root4,value='')
#创建文本组件框\标签组件
label=Label(root4,text="学号",font=("微软雅黑 -20"))
label.place(x=130,y=30,height=40,width=80)
selId=Entry((root4),textvariable=sId)
selId.place(x=190,y=30,height=40,width=200)
#创建关联字符变量
varName=StringVar(root4,value='')
varId=StringVar(root4,value='')
varClass=StringVar(root4,value='')
varAge=StringVar(root4,value='')
#创建标签组件
label=Label(root4,text="姓名:",font=("微软雅黑 -20"))
#label.grid(row=0,sticky=E)
label.place(x=130,y=160,height=40,width=80)
label=Label(root4,text="学号:",font=("微软雅黑 -20"))
#label.grid(row=1,sticky=E)
label.place(x=130,y=210,height=40,width=80)
label=Label(root4,text="班级:",font=("微软雅黑 -20"))
#label.grid(row=2,sticky=E)
label.place(x=130,y=260,height=40,width=80)
label=Label(root4,text="年龄:",font=("微软雅黑 -20"))
#label.grid(row=3,sticky=E)
label.place(x=130,y=310,height=40,width=80)
#创建文本框组件,同时设置关联的变量
# 姓名entryName
# 学号entryId
# 班级entryClass
# 年龄entryAge
entryName=Entry((root4),textvariable=varName)
#entryName.grid(row=0,column=1,sticky=W)
entryName.place(x=190,y=160,height=40,width=200)
entryId=Entry((root4),textvariable=varId)
#entryId.grid(row=1,column=1,sticky=W)
entryId.place(x=190,y=210,height=40,width=200)
entryClass=Entry((root4),textvariable=varClass)
#entryClass.grid(row=2,column=1,sticky=W)
entryClass.place(x=190,y=260,height=40,width=200)
entryAge=Entry((root4),textvariable=varAge)
#entryAge.grid(row=3,column=1,sticky=W)
entryAge.place(x=190,y=310,height=40,width=200)
def select():
conn = sqlite3.connect('student1.db')
stu_id = eval(selId.get())#学号输入
cursor = conn.execute("SELECT * from StudentTable where StuId = %d;"%stu_id)
conn.commit()
for row in cursor:
if stu_id == row[1]:
stu_name=row[2]
stu_class=row[3]
stu_age=row[4]
varName.set(stu_name)
varId.set(stu_id)
varClass.set(stu_class)
varAge.set(stu_age)
def saveName():
name=entryName.get()
conn=sqlite3.connect('student1.db')
sql="UPDATE StudentTable SET NAME='%s' WHERE StuId=%d;"%(name,eval(selId.get()))
conn.execute(sql)
conn.commit()
messagebox.showinfo(title='恭喜',message='保存成功!')
def saveCla():
cla=eval(entryClass.get())
conn=sqlite3.connect('student1.db')
sql="UPDATE StudentTable SET CLASS=%d WHERE StuId=%d;"%(cla,eval(selId.get()))
conn.execute(sql)
conn.commit()
messagebox.showinfo(title='恭喜',message='保存成功!')
def saveAge():
age=eval(entryAge.get())
conn=sqlite3.connect('student1.db')
sql="UPDATE StudentTable SET AGE=%d WHERE StuId=%d;"%(age,eval(selId.get()))
conn.execute(sql)
conn.commit()
messagebox.showinfo(title='恭喜',message='保存成功!')
#保存键
buttonname=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveName)
buttonname.place(x=420,y=160,height=40,width=60)
buttoncla=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveCla)
buttoncla.place(x=420,y=260,height=40,width=60)
buttonage=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveAge)
buttonage.place(x=420,y=310,height=40,width=60)
def cancel():
sId.set('')
#取消键
buttoncancel=Button(root4,text="取消",font=("微软雅黑 -20"),command=cancel)
buttoncancel.place(x=100,y=80,height=40,width=60)
#查询键
buttonselect=Button(root4,text="查询",font=("微软雅黑 -20"),command=select)
buttonselect.place(x=230,y=80,height=40,width=60)
#退出键
buttondel=Button(root4,text="退出",font="微软雅黑 -20",command=root4.destroy)
buttondel.place(x=350,y=80,height=40,width=60)
root4.mainloop()
#创建顶级菜单及其下拉菜单
menubar=Menu(root)
filemenu=Menu(menubar,tearoff=False)
filemenu.add_command(label="增加",command=insert_stu)
filemenu.add_command(label="删除",command=delete_stu)#command接删除函数/下面接修改函数
filemenu.add_command(label="修改",command=change_stu)
filemenu.add_command(label="查询",command=sel_stu)
filemenu.add_separator()
filemenu.add_command(label="退出",command=root.destroy)
menubar.add_cascade(label="菜单",menu=filemenu)
#显示菜单
root.config(menu=menubar)
buttoninsert_stu=Button(root,text="录入学生信息",font=("微软雅黑 -20"),command=insert_stu)
#buttoninsert_stu.grid(row=2,column=0)由下面的代码将该代码覆盖,显示的是在界面上的位置
buttoninsert_stu.place(x=200,y=50,height=40,width=200)
buttondelete_stu=Button(root,text="删除学生信息",font=("微软雅黑 -20"),command=delete_stu)
#buttondelete_stu.grid(row=2,column=1)
buttondelete_stu.place(x=200,y=150,height=40,width=200)
buttonchange_stu=Button(root,text="修改学生信息",font=("微软雅黑 -20"),command=change_stu)
#buttonchange_stu.grid(row=4,column=0)
buttonchange_stu.place(x=200,y=250,height=40,width=200)
buttonsel_stu=Button(root,text="查询学生信息",font=("微软雅黑 -20"),command=sel_stu)
#buttonsel_stu.grid(row=4,column=1)
buttonsel_stu.place(x=200,y=350,height=40,width=200)
root.mainloop()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署