python编写学生信息管理系统_python编写学生信息管理系统的增删改查功能

1 from tkinter import *

2 from tkinter importmessagebox3 from tkinter importfiledialog4 from PIL importImage,ImageTk5 importtkinter6 importsqlite37

8

9

10 #创建本地数据库

11 #提交的文件中存在数据库,所以该数据库的创建程序可以不运行

12

13 """

14

15

16 conn.execute ('''CREATE TABLE StudentTable(17 ID INTEGER PRIMARY KEY AUTOINCREMENT,18 StuId INTEGER NOT NULL,19 NAME TEXT NOT NULL,20 CLASS INT NOT NULL,21 AGE INTEGER Not NULL);''')22 print("Table created successfully");23

24 #创建本地数据库25

26

27

28

29 """

30 #打开本地数据库用于存储用户信息

31 conn = sqlite3.connect('student1.db')32 #主界面

33 root=Tk()34 root.title("学生信息管理系统")35 root.config(width=600)36 root.config(height=600)37

38 #添加窗口背景图片

39 canvas=tkinter.Canvas(root,40 width=600, #指定Canvas组件的宽度

41 height=600, #指定Canvas组件的高度

42 bg='white' #指定Canvas组件的背景色

43 #im=tkinter.PhotoImage(file='img.gif') 使用PhotoImage打开图片

44

45

46 )47

48

49

50 """记得在运行时修改文件所在位置。*********************************************************"""

51

52

53 image=Image.open("E:\爱心.jpg")54 im=ImageTk.PhotoImage(image)55

56 canvas.create_image(400,200,image=im) #使用creat_image将图片添加到Canvas

57 canvas.pack()58

59

60

61 """******************************************************************************************"""

62

63 """************************ 录入信息部分 ********************************************"""

64

65

66

67

68 #增加学生信息

69 def insert_stu(): #录入学生信息

70 root1=Tk()71 root1.title("录入学生信息")72 root1.config(width=600)73 root1.config(height=600)74

75

76

77

78

79

80 #创建关联字符变量

81 varName=StringVar(root1,value='')82 varId=StringVar(root1,value='')83 varClass=StringVar(root1,value='')84 varAge=StringVar(root1,value='')85

86

87

88 #创建标签组件

89 label=Label(root1,text="姓名:",font=("微软雅黑 -20"))90 #label.grid(row=0,sticky=E)

91 label.place(x=30,y=60,height=40,width=80)92

93 label=Label(root1,text="学号:",font=("微软雅黑 -20"))94 #label.grid(row=1,sticky=E)

95 label.place(x=30,y=110,height=40,width=80)96

97

98 label=Label(root1,text="班级:",font=("微软雅黑 -20"))99 #label.grid(row=2,sticky=E)

100 label.place(x=30,y=160,height=40,width=80)101

102

103 label=Label(root1,text="年龄:",font=("微软雅黑 -20"))104 #label.grid(row=3,sticky=E)

105 label.place(x=30,y=210,height=40,width=80)106

107 #创建文本框组件,同时设置关联的变量

108 #姓名entryName

109 #学号entryId

110 #班级entryClass

111 #年龄entryAge

112

113

114 entryName=Entry((root1),textvariable=varName)115 #entryName.grid(row=0,column=1,sticky=W)

116 entryName.place(x=120,y=60,height=40,width=200)117

118 entryId=Entry((root1),textvariable=varId)119 #entryId.grid(row=1,column=1,sticky=W)

120 entryId.place(x=120,y=110,height=40,width=200)121

122 entryClass=Entry((root1),textvariable=varClass)123 #entryClass.grid(row=2,column=1,sticky=W)

124 entryClass.place(x=120,y=160,height=40,width=200)125

126 entryAge=Entry((root1),textvariable=varAge)127 #entryAge.grid(row=3,column=1,sticky=W)

128 entryAge.place(x=120,y=210,height=40,width=200)129

130

131 defbuttonOK():132 i=0133

134 conn = sqlite3.connect('student1.db')135

136 stu_id = eval(entryId.get())#学号输入

137 stu_name =str(entryName.get())#姓名录入

138 stu_class =eval(entryClass.get())#班级录入

139 stu_age=eval(entryAge.get())#年龄录入

140

141 cursor = conn.execute("SELECT * from StudentTable;")142 conn.commit()143 for row in cursor:#进行遍历查找是否有重复的学号

144 if stu_id==row[0]:145 i=1

146 break

147 else:148 i=0149 #查找完成若有重复的学号,则警告。否则录入数据库

150 if i==1:151 messagebox.showerror('警告',message='学号重复,请重新输入')152 else:153 try:154 sql1 = "INSERT INTO StudentTable(StuId,NAME,CLA,AGE)"

155 sql1+="VALUES(%d,'%s',%d,%d)"%(stu_id,stu_name,stu_class,stu_age)156 conn.execute(sql1)157 conn.commit()158 messagebox.showinfo(title='恭喜',message='录入成功!')159 root1.destroy()160 except:161 messagebox.showerror('警告',message='未录入成功')162

163

164 buttonbuttonOK=Button(root1,text="录入学生信息",font=("微软雅黑 -20"),command=buttonOK)165 buttonbuttonOK.place(x=150,y=300,height=40,width=200)166 defcancel():167 varName.set('')168 varId.set('')169 varClass.set('')170 varAge.set('')171

172

173 #取消键

174 buttonCancel=Button(root1,text="取消",font=("微软雅黑 -20"),command=cancel)175 buttonCancel.place(x=150,y=350,height=40,width=200)176

177 #退出键

178 buttondel=Button(root1,text="退出",font=("微软雅黑 -20"),command=root1.destroy)179 buttondel.place(x=150,y=400,height=40,width=200)180 root1.mainloop()181

182

183

184 #录入完成

185 """******************************************************************************************"""

186

187 """************************ 删除的部分 ********************************************"""

188 #删除学生信息

189

190

191

192 defdelete_stu():193 root2=Tk()194 root2.title("删除学生信息")195 root2.config(width=600)196 root2.config(height=600)197

198

199 #添加窗口背景图片

200

201

202

203 #创建标签

204 label=Label(root2,text="学号:",font=("微软雅黑 -20"))205 #label.grid(row=1,sticky=E)

206 label.place(x=30,y=20,height=40,width=80)207

208 entryId=Entry(root2)209 entryId.place(x=120,y=20,height=40,width=200)210

211 defdelete():212

213 conn = sqlite3.connect('student1.db')214 stu_id = eval(entryId.get())#学号输入

215 conn.execute("DELETE from StudentTable where StuId = '%s';"%stu_id)216 conn.commit()217 messagebox.showinfo(title='恭喜',message='删除成功!')218 root2.destroy()219

220

221

222

223

224 #删除键

225 buttondelete=Button(root2,text="删除",font=("微软雅黑 -20"),command=delete)226 buttondelete.place(x=150,y=160,height=40,width=200)227

228 #退出键

229 buttondel=Button(root2,text="退出",font=("微软雅黑 -20"),command=root2.destroy)230 buttondel.place(x=150,y=210,height=40,width=200)231

232

233 root2.mainloop()234 #删除完成

235 """******************************************************************************************"""

236

237 """************************ 查询的部分 ********************************************"""

238

239

240

241 #查询学生信息

242 defsel_stu():243 root3=Tk()244 root3.title("查询学生信息")245 root3.config(width=600)246 root3.config(height=600)247

248

249

250

251 #创建关联变量

252 sId=StringVar(root3,value='')253

254 #创建文本组件框\标签组件

255 label=Label(root3,text="学号",font=("微软雅黑 -20"))256 label.place(x=30,y=10,height=40,width=80)257

258 selId=Entry((root3),textvariable=sId)259 selId.place(x=120,y=10,height=40,width=200)260

261

262

263 defselect():264

265 #创建关联字符变量

266 varName=StringVar(root3,value='')267 varId=StringVar(root3,value='')268 varClass=StringVar(root3,value='')269 varAge=StringVar(root3,value='')270

271

272 conn = sqlite3.connect('student1.db')273 stu_id = eval(selId.get())#学号输入

274 cursor = conn.execute("SELECT * from StudentTable where StuId = '%d';"%stu_id)275 conn.commit()276 for row incursor:277 if stu_id ==row[0]:278 stu_name=row[1]279 stu_class=row[2]280 stu_age=row[3]281

282

283

284

285 #创建标签组件

286 label=Label(root3,text="姓名:",font=("微软雅黑 -20"))287 #label.grid(row=0,sticky=E)

288 label.place(x=30,y=110,height=40,width=80)289

290 label=Label(root3,text="学号:",font=("微软雅黑 -20"))291 #label.grid(row=1,sticky=E)

292 label.place(x=30,y=160,height=40,width=80)293

294

295 label=Label(root3,text="班级:",font=("微软雅黑 -20"))296 #label.grid(row=2,sticky=E)

297 label.place(x=30,y=210,height=40,width=80)298

299

300 label=Label(root3,text="年龄:",font=("微软雅黑 -20"))301 #label.grid(row=3,sticky=E)

302 label.place(x=30,y=260,height=40,width=80)303

304 #创建文本框组件,同时设置关联的变量

305 #姓名entryName

306 #学号entryId

307 #班级entryClass

308 #年龄entryAge

309

310 entryName=Entry((root3),textvariable=varName)311 #entryName.grid(row=0,column=1,sticky=W)

312 entryName.place(x=120,y=110,height=40,width=200)313

314 entryId=Entry((root3),textvariable=varId)315 #entryId.grid(row=1,column=1,sticky=W)

316 entryId.place(x=120,y=160,height=40,width=200)317

318 entryClass=Entry((root3),textvariable=varClass)319 #entryClass.grid(row=2,column=1,sticky=W)

320 entryClass.place(x=120,y=210,height=40,width=200)321

322 entryAge=Entry((root3),textvariable=varAge)323 #entryAge.grid(row=3,column=1,sticky=W)

324 entryAge.place(x=120,y=260,height=40,width=200)325

326 varName.set(stu_name)327 varId.set(stu_id)328 varClass.set(stu_class)329 varAge.set(stu_age)330

331 #查询键

332 buttοnselect=Button(root3,text="查询",font=("微软雅黑 -20"),command=select)333 buttonselect.place(x=200,y=60,height=40,width=100)334

335 #取消键

336 defcancel():337 sId.set('')338

339 buttoncancel=Button(root3,text="取消",font="微软雅黑 -20",command=cancel)340 buttoncancel.place(x=50,y=60,height=40,width=100)341

342 #退出键

343 buttondel=Button(root3,text="退出",font="微软雅黑 -20",command=root3.destroy)344 buttondel.place(x=350,y=60,height=40,width=100)345 root3.mainloop()346

347

348 #查询完成

349 """******************************************************************************************"""

350

351 """************************ 修改的部分 ********************************************"""

352

353

354 #修改学生信息

355 defchange_stu():356 root4=Tk()357 root4.title("修改学生信息")358 root4.config(width=600)359 root4.config(height=600)360

361 #创建关联变量

362 sId=StringVar(root4,value='')363

364 #创建文本组件框\标签组件

365 label=Label(root4,text="学号",font=("微软雅黑 -20"))366 label.place(x=30,y=10,height=40,width=80)367

368 selId=Entry((root4),textvariable=sId)369 selId.place(x=120,y=10,height=40,width=200)370

371 #创建关联字符变量

372 varName=StringVar(root4,value='')373 varId=StringVar(root4,value='')374 varClass=StringVar(root4,value='')375 varAge=StringVar(root4,value='')376

377 #创建标签组件

378

379 label=Label(root4,text="姓名:",font=("微软雅黑 -20"))380 #label.grid(row=0,sticky=E)

381 label.place(x=30,y=110,height=40,width=80)382

383 label=Label(root4,text="学号:",font=("微软雅黑 -20"))384 #label.grid(row=1,sticky=E)

385 label.place(x=30,y=160,height=40,width=80)386

387

388 label=Label(root4,text="班级:",font=("微软雅黑 -20"))389 #label.grid(row=2,sticky=E)

390 label.place(x=30,y=210,height=40,width=80)391

392

393 label=Label(root4,text="年龄:",font=("微软雅黑 -20"))394 #label.grid(row=3,sticky=E)

395 label.place(x=30,y=260,height=40,width=80)396

397 #创建文本框组件,同时设置关联的变量

398 #姓名entryName

399 #学号entryId

400 #班级entryClass

401 #年龄entryAge

402 entryName=Entry((root4),textvariable=varName)403 #entryName.grid(row=0,column=1,sticky=W)

404 entryName.place(x=120,y=110,height=40,width=200)405

406 entryId=Entry((root4),textvariable=varId)407 #entryId.grid(row=1,column=1,sticky=W)

408 entryId.place(x=120,y=160,height=40,width=200)409

410 entryClass=Entry((root4),textvariable=varClass)411 #entryClass.grid(row=2,column=1,sticky=W)

412 entryClass.place(x=120,y=210,height=40,width=200)413

414 entryAge=Entry((root4),textvariable=varAge)415 #entryAge.grid(row=3,column=1,sticky=W)

416 entryAge.place(x=120,y=260,height=40,width=200)417

418 defselect():419

420 conn = sqlite3.connect('student1.db')421 stu_id = eval(selId.get())#学号输入

422 cursor = conn.execute("SELECT * from StudentTable where StuId = %d;"%stu_id)423 conn.commit()424 for row incursor:425 if stu_id ==row[0]:426 stu_name=row[1]427 stu_class=row[2]428 stu_age=row[3]429

430 varName.set(stu_name)431 varId.set(stu_id)432 varClass.set(stu_class)433 varAge.set(stu_age)434

435 defsaveName():436 name=entryName.get()437 conn=sqlite3.connect('student1.db')438 sql="UPDATE StudentTable SET NAME='%s' WHERE StuId=%d;"%(name,eval(selId.get()))439 conn.execute(sql)440 conn.commit()441

442 messagebox.showinfo(title='恭喜',message='保存成功!')443

444 defsaveCla():445 cla=eval(entryClass.get())446 conn=sqlite3.connect('student1.db')447 sql="UPDATE StudentTable SET CLA=%d WHERE StuId=%d;"%(cla,eval(selId.get()))448 conn.execute(sql)449 conn.commit()450

451 messagebox.showinfo(title='恭喜',message='保存成功!')452

453 defsaveAge():454 age=eval(entryAge.get())455 conn=sqlite3.connect('student1.db')456 sql="UPDATE StudentTable SET AGE=%d WHERE StuId=%d;"%(age,eval(selId.get()))457 conn.execute(sql)458 conn.commit()459

460 messagebox.showinfo(title='恭喜',message='保存成功!')461

462

463 #保存键

464 buttonname=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveName)465 buttonname.place(x=330,y=110,height=40,width=60)466

467 buttoncla=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveCla)468 buttoncla.place(x=330,y=210,height=40,width=60)469

470 buttonage=Button(root4,text="保存",font=("微软雅黑 -20"),command=saveAge)471 buttonage.place(x=330,y=260,height=40,width=60)472

473 defcancel():474 sId.set('')475

476 #取消键

477 buttoncancel=Button(root4,text="取消",font=("微软雅黑 -20"),command=cancel)478 buttoncancel.place(x=20,y=60,height=40,width=60)479

480 #查询键

481 buttοnselect=Button(root4,text="查询",font=("微软雅黑 -20"),command=select)482 buttonselect.place(x=100,y=60,height=40,width=60)483

484 #退出键

485 buttondel=Button(root4,text="退出",font="微软雅黑 -20",command=root4.destroy)486 buttondel.place(x=260,y=60,height=40,width=60)487

488 root4.mainloop()489

490 #创建顶级菜单及其下拉菜单

491 menubar=Menu(root)492 filemenu=Menu(menubar,tearoff=False)493 filemenu.add_command(label="增加",command=insert_stu)494 filemenu.add_command(label="删除",command=delete_stu)#command接删除函数/下面接修改函数

495 filemenu.add_command(label="修改",command=change_stu)496 filemenu.add_command(label="查询",command=sel_stu)497

498 filemenu.add_separator()499 filemenu.add_command(label="退出",command=root.destroy)500 menubar.add_cascade(label="菜单",menu=filemenu)501

502 #显示菜单

503 root.config(menu=menubar)504

505 buttoninsert_stu=Button(root,text="录入学生信息",font=("微软雅黑 -20"),command=insert_stu)506 #buttoninsert_stu.grid(row=2,column=0)由下面的代码将该代码覆盖,显示的是在界面上的位置

507 buttoninsert_stu.place(x=50,y=50,height=40,width=200)508

509 buttondelete_stu=Button(root,text="删除学生信息",font=("微软雅黑 -20"),command=delete_stu)510 #buttondelete_stu.grid(row=2,column=1)

511 buttondelete_stu.place(x=50,y=150,height=40,width=200)512

513 buttonchange_stu=Button(root,text="修改学生信息",font=("微软雅黑 -20"),command=change_stu)514 #buttonchange_stu.grid(row=4,column=0)

515 buttonchange_stu.place(x=50,y=250,height=40,width=200)516

517 buttonsel_stu=Button(root,text="查询学生信息",font=("微软雅黑 -20"),command=sel_stu)518 #buttonsel_stu.grid(row=4,column=1)

519 buttonsel_stu.place(x=50,y=350,height=40,width=200)520 root.mainloop()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值