最近学习了Python的tkinter的gui库,于是决定做一个gui版的学生管理系统程序
原本的代码是:https://blog.csdn.net/weixin_43321726/article/details/103569640
这是我的代码,花了半天时间敲出来的
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.messagebox import showinfo
import json
import time
import os
import cure
root = tk.Tk()
root.title('学生信息管理系统')
#背景
canvas = tk.Canvas(root, width=1200,height=699,bd=0, highlightthickness=0)
imgpath = r'D:\py\vs\Python_package\wordpress.jpg'
img = Image.open(imgpath)
photo = ImageTk.PhotoImage(img)
canvas.create_image(700, 500, image=photo)
canvas.pack()
canvas.create_text(600,350,text='欢 迎 使 用 学 生 信 息 管 理 系 统',font=('微软雅黑',26))
def post(name,math,chinese,english,win1):
name = name.get()
math = math.get()
chinese = chinese.get()
english = english.get()
try:
total = int(math) + int(chinese) + int(english)
except:
tk.messagebox.showerror('oh 出错了','提交失败,请输入正确的成绩')
btn1cmd()
else:
grade_list = {'name':name,'math':math,'chinese':chinese,'english':english,'total':total}
print([name,math,chinese,english,total])
print('姓名:{}\t数学成绩:{}\t语文成绩:{}\t英语成绩: {}'.format(name,math,chinese,english,total))
with open('Python_package\grade list.txt','a',encoding='utf-8') as f:
f.write('\n'+str(grade_list))
tk.messagebox.showinfo('提交成功','提交成功,即将返回主页')
time.sleep(3)
win1.destroy()
pass
def find(name):
name = name.get()
with open('Python_package\grade list.txt',encoding='utf-8') as f:
lines = f.readlines()
for i in range(len(lines)):
if name in lines[i]:
tk.messagebox.showinfo('找到了',lines[i])
break
else:
tk.messagebox.showerror('error','此学生不存在,请重新输入')
def delGakuse(name):
name = name.get()
with open('Python_package\grade list.txt',encoding='utf-8') as f:
lines = f.readlines()
for i in lines:
if (eval(i))['name'] == name:
tk.messagebox.showinfo('OK','已成功删除信息')
conversion('Python_package\grade list.txt',lines.index(i),'')
break
else:
tk.messagebox.showerror('error','此学生不存在')
def edit(name):
name = name.get()
with open('Python_package\grade list.txt',encoding='utf-8') as f:
lines = f.readlines()
for i in range(len(lines)):
if name in lines[i]:
def a(r,a):#懒得起名字了
mg = ''
mg += str(r.get())
数学
edit = tk.Toplevel()
edit.title('请选择你要修改的内容')
r=tk.IntVar()
r.set(2)
tk.Radiobutton(edit,text='数学',value=1,variable=r).pack()
tk.Radiobutton(edit,text='语文',value=2,variable=r).pack()
tk.Radiobutton(edit,text='英语',value=3,variable=r).pack()
a=tk.StringVar()
a.set('输入分数')
tk.Entry(width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=a, validate="focus").pack()
tk.Button(edit,text='修改',command=lambda: a(r,a)).pack()
conversion('Python_package\grade list.txt',i,'')
break
else:
tk.messagebox.showerror('error','此学生不存在,请重新输入')
def btn1cmd():
win1 = tk.Toplevel()
win1.title('新建学生信息')
name = tk.StringVar()
name.set('请输入名字')
tk.Entry(win1, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=name, validate="focus").pack()
math = tk.StringVar()
math.set('请输入数学成绩')
tk.Entry(win1, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=math, validate="focus").pack()
chinese = tk.StringVar()
chinese.set('请输入语文成绩')
tk.Entry(win1, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=chinese, validate="focus").pack()
english = tk.StringVar()
english.set('请输入英语成绩')
tk.Entry(win1, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=english, validate="focus").pack()
tk.Button(win1,text='提交',command=lambda: post(name,math,chinese,english,win1)).pack()
def btn2cmd():
with open('Python_package\grade list.txt',encoding='utf-8') as f:
grade_list = f.read()
win2 = tk.Toplevel()
win2.title('显示全部信息')
tk.Label(win2,text=grade_list).pack()
def btn3cmd():
win3 = tk.Toplevel()
win3.title('查询学生信息')
name = tk.StringVar()
name.set('请输入你要查询学生的姓名')
tk.Entry(win3, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=name, validate="focus").pack()
tk.Button(win3,text='查找',command=lambda: find(name)).pack()
def btn4cmd():
win4 = tk.Toplevel()
win4.title('删除学生信息')
name = tk.StringVar()
name.set('请输入你要删除学生的姓名')
tk.Entry(win4, width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=name, validate="focus").pack()
tk.Button(win4,text='删除',command=lambda: delGakuse(name)).pack()
def btn5cmd():
win5 = tk.Toplevel()
win5.title('修改学生信息')
name = tk.StringVar()
name.set('请输入你要查询学生的姓名')
tk.Entry(win5,width=30, borderwidth=1, fg="#f00",insertwidth=1,
insertbackground="#333", state=tk.NORMAL,
textvariable=name, validate="focus").pack()
tk.Button(win5,text='查询',command=lambda: edit(name)).pack()
def start(canvas):
canvas.pack_forget()
theLabel = tk.Label(root,text='''
******************************
欢迎使用【学生信息管理系统】
******************************
''')
root.geometry('700x500')
theLabel.pack()
tk.Button(root,text="1.新建学生信息",command=btn1cmd).pack()
tk.Button(root,text="2.显示全部信息",command=lambda: btn2cmd()).pack()
tk.Button(root,text="3.查询学生信息",command=btn3cmd).pack()
tk.Button(root,text="4.删除学生信息",command=btn4cmd).pack()
tk.Button(root,text="5.修改学生信息",command=btn5cmd).pack()
startBtn = tk.Button(root, text = "Start!",anchor = tk.W,command=lambda: start(canvas))
startBtn.configure(width = 10, activebackground = "#33B5E5", relief = tk.SOLID,bg='pink')
button1_window = canvas.create_window(560, 400,window=startBtn,anchor='nw')
def on_closing():
if tk.messagebox.askokcancel("Quit", '真的要离开吗~QAQ'):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
里面有一个我自制的库,方便写代码的时候用上,下载地址
https://gitee.com/cat_ci/codes/1y2sqou3zvjbtx6nk4dl896
如果get到了你想要的东西,别忘留下一个赞哟~