赞
踩
,今天跟大家分享一下一个python期末大作业的开发思路。大家可以参考思路自己修改完善。我们讲解的是学生信息管理系统。界面还是用tkinter做,数据库使用sqlite。
先看一下软件截图
数据库设计
编写过程
1.导库
from tkinter import * from tkinter.messagebox import askokcancel, showinfoimport tkinter.ttk as ttk from sqlite3 import *
- 1
- 2
- 3
2.初始化
root = Tk()systitle = '学生信息管理系统' # 系统标题 dbfile = 'student.db' # v2 cn = connect(dbfile) # v2连接数据库,连接对象cn operateFrame = Frame() # 功能窗口 operateFrame.pack()
- 1
- 2
- 3
- 4
- 5
3.定义软件主界面函数
def main(): root.geometry('600x400') # 设置窗口初始大小 root.title(systitle) # 设置系统标题 # 创建系统菜单 menubar = Menu(root) # 创建Menu对象menubar,将作为root窗口中的菜单 root.config(menu=menubar) # 将menubar菜单作为root窗口的顶层菜单栏 # menuStudent将作为menubar菜单的子菜单 menuStudent = Menu(menubar, tearoff=0) menuStudent.add_command(label='添加新学生', font=('宋体', 10), command=addStudent) menuStudent.add_command(label='显示全部学生信息', font=('宋体', 10), command=showAllStudent) menuStudent.add_command(label='查找/修改/删除学生信息', font=('宋体', 10), command=checkUpdateStudent) menuStudent.add_separator() menuStudent.add_command(label='退出', font=('宋体', 10), command='') # 菜单file添加为menubar的子菜单 menubar.add_cascade(label='学生管理', font=('宋体', 10), menu=menuStudent) menuHelp = Menu(menubar, tearoff=0) # help将作为menubar菜单的子菜单 menuHelp.add_command(label='查看日志', font=('宋体', 10), command='') menuHelp.add_command(label='关于...', font=('宋体', 10), command='') # 菜单help添加为menubar的子菜单 menubar.add_cascade(label='其他', font=('宋体', 10), menu=menuHelp) root.mainloop()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
4.定义添加学生信息子界面函数
def addStudent(): for widget in operateFrame.winfo_children(): # 清空窗口中原有的所有内容 widget.destroy() f1 = Frame(operateFrame) f1.pack() studentIdVar = StringVar() studentNameVar = StringVar() ageVar = StringVar() telVar = StringVar() lStudentId = Label(f1, text='学号:') lStudentName = Label(f1, text='姓名:') lAge = Label(f1, text='年龄:') lTel = Label(f1, text='电话:') tStudentId = Entry(f1, textvariable=studentIdVar) tStudentName = Entry(f1, textvariable=studentNameVar) tAge = Entry(f1, textvariable=ageVar) tTel = Entry(f1, textvariable=telVar) lStudentId.grid(row=1, column=1) lStudentName.grid(row=2, column=1) lAge.grid(row=3, column=1) lTel.grid(row=4, column=1) tStudentId.grid(row=1, column=2) tStudentName.grid(row=2, column=2) tAge.grid(row=3, column=2) tTel.grid(row=4, column=2) f2 = Frame(operateFrame) f2.pack(pady=20) bReset = Button(f2, text='重置') bSave = Button(f2, text='保存') bReset.grid(row=1, column=1) bSave.grid(row=1, column=2) def reset(): studentIdVar.set('') studentNameVar.set('') ageVar.set('') telVar.set('') bReset.config(command=reset) def save():#event try: id = studentIdVar.get() if not id.isdigit(): raise Exception('学号必须为数字!') elif len(id) != 4: raise Exception('学号必须是4位!') name = studentNameVar.get() age = ageVar.get() if not age.isdigit(): raise Exception('年龄错误!') else: age = int(age) if age < 15 or age > 50: raise Exception('年龄必须大于15小于50!') tel = telVar.get() if not tel.isdigit(): raise Exception('电话错误!') elif len(tel) != 4: raise Exception('电话必须4位!') # 向数据库发送插入指令 cn.execute('INSERT INTO student VALUES(?,?,?,?)', (id, name, age, tel)) cn.commit() showinfo(systitle, '成功添加新学生!') reset() tStudentId.focus() # 测试:向数据库发一个查询指令 # 获得了一个列表,列表中每个单元是一个学生,每个学生是一个元组 # stulist = cn.execute('SELECT * FROM student').fetchall() # print(stulist) except Exception as ex: showinfo(systitle, ex) bSave.config(command=save) tTel.bind('<Return>', save)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
5.定义显示所有学生信息子界面函数
# 6 显示所有学生 def showAllStudent(): #v3 for widget in operateFrame.winfo_children(): # 清空窗口中原有的所有内容 widget.destroy() operateFrame.columnconfigure(1, minsize=50) operateFrame.columnconfigure(2, minsize=100) operateFrame.columnconfigure(3, minsize=50) operateFrame.columnconfigure(4, minsize=100) Label(operateFrame, text='学号',font=('宋体', 10, 'bold')).grid(row=0, column=1) Label(operateFrame, text='姓名',font=('宋体', 10, 'bold')).grid(row=0, column=2) Label(operateFrame, text='年龄',font=('宋体', 10, 'bold')).grid(row=0, column=3) Label(operateFrame, text='电话',font=('宋体', 10, 'bold')).grid(row=0, column=4) stulist = cn.execute('select * from student').fetchall() rownum = 1 for stu in stulist: colnum = 1 for info in stu: Label(operateFrame, text=str(info),font=('宋体', 10)).grid(row=rownum, column=colnum) colnum += 1 rownum += 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
6.定义查找/修改/删除学生信息子界面函数
# 7. 查找删除修改学生 def checkUpdateStudent(): #v4 for widget in operateFrame.winfo_children(): # 清空窗口中原有的所有内容 widget.destroy() f1 = LabelFrame(operateFrame,text='查找学生:') f1.pack() studentIdToSearchVar = StringVar() Label(f1, text='请输入要查找的学生学号:').grid(row=1, column=1) tStudentIdToSearch = Entry(f1,textvariable=studentIdToSearchVar) tStudentIdToSearch.grid(row=1, column=2) bSearch = Button(f1, text='查询') bSearch.grid(row=1, column=3) f2 = LabelFrame(operateFrame,text='删除与修改学生:') f2.pack(pady=30) fDelete = Frame(f2) fDelete.pack() bDelete = Button(fDelete,text='删除学生',state=DISABLED) bDelete.pack() fUpdate = Frame(f2) fUpdate.pack() studentIdVar = StringVar() studentNameVar = StringVar() ageVar = StringVar() telVar = StringVar() Label(fUpdate,text='学号:').grid(row=1,column=1) Label(fUpdate, text='姓名:').grid(row=2, column=1) Label(fUpdate, text='年龄:').grid(row=3, column=1) Label(fUpdate, text='电话:').grid(row=4, column=1) Entry(fUpdate,textvariable=studentIdVar,state=DISABLED).grid(row=1,column=2) Entry(fUpdate,textvariable=studentNameVar).grid(row=2, column=2) Entry(fUpdate,textvariable=ageVar).grid(row=3, column=2) Entry(fUpdate,textvariable=telVar).grid(row=4, column=2) bSave = Button(fUpdate,text='保存修改',state=DISABLED) bSave.grid(row=4,column=3) def search(event=None): studentIdVar.set('') studentNameVar.set('') ageVar.set('') telVar.set('') studentIdToSearch = studentIdToSearchVar.get() print(type(studentIdToSearch ),studentIdToSearch ) stu = cn.execute('SELECT sid,sname,age,tel FROM student WHERE sid=?' ,(studentIdToSearch,)).fetchone() print(stu) if stu == None: showinfo(systitle,'该学生不存在') bSave.config(state=DISABLED) bDelete.config(state=DISABLED) else: studentIdVar.set(stu[0]) studentNameVar.set(stu[1]) ageVar.set(stu[2]) telVar.set(stu[3]) bDelete.config(state=NORMAL) bSave.config(state=NORMAL) bSearch.config(command=search) tStudentIdToSearch.bind("<Return>",search) def delete(): cn.execute('DELETE FROM student WHERE sid=?',(studentIdVar.get(),)) cn.commit() showinfo(systitle,'成功删除!') studentIdVar.set('') studentNameVar.set('') ageVar.set('') telVar.set('') bDelete.config(command=delete) def save(): cn.execute('UPDATE student SET sname=?,age=?,tel=? WHERE sid=?' ,(studentNameVar.get(),ageVar.get(),telVar.get(),studentIdVar.get())) cn.commit() showinfo(systitle,'成功修改!') bSave.config(command=save)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
7.在程序主入口调用main()函数,启动软件主界面。
if __name__ == '__main__': main()
- 1
.get(),studentIdVar.get()))
cn.commit() showinfo(systitle,'成功修改!') bSave.config(command=save)
- 1
- 2
- 3
7.在程序主入口调用main()函数,启动软件主界面。
- 1
- 2
- 3
if name == ‘main’: main()
- 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。