编写图形界面应用程序实现学生信息的添加、查询和删除学生信息存储在d dstudentstxt文件。要求:1单击添加按钮可以将输入框的学号、姓名、性别、年龄信息添加到文件中。2单击查询根据输入框内的学号查询对应学生的信息并显示在文本框。如不存在则弹出提示框。3单击删除根据输入框内的学号删除该学生在文件中的信息。如该学号不存在则弹出提示框。4单击清除将文本框的内容清空。python编写import t
修改代码,实现删除功能
import tkinter as tk import tkinter.messagebox as messagebox
window = tk.Tk() window.title("学生信息添加和查询系统") window.geometry("320x240")
noStuLabel = tk.Label(window,text = "学号") noStuLabel.place(x=20, y=20, width=80, height=30)
nameStuLabel = tk.Label(window,text="姓名") nameStuLabel.place(x=20, y=60, width=80, height=30)
sexStuLabel = tk.Label(window,text="性别") sexStuLabel.place(x=20, y=100, width=80, height=30)
ageStuLabel = tk.Label(window,text="年龄") ageStuLabel.place(x=20, y=140, width=80, height=30)
noVar = tk.StringVar(value="") nameVar = tk.StringVar(value="") sexVar = tk.StringVar(value="") ageVar = tk.StringVar(value="")
noStuEntry = tk.Entry(window,textvariable=noVar) nameStuEntry = tk.Entry(window,textvariable=nameVar) sexStuEntry = tk.Entry(window,textvariable=sexVar) ageStuEntry = tk.Entry(window,textvariable=ageVar)
noStuEntry.place(x=130, y=20, width=120, height=30) nameStuEntry.place(x=130, y=60, width=120, height=30) sexStuEntry.place(x=130, y=100, width=120, height=30) ageStuEntry.place(x=130, y=140, width=120, height=30)
def add(): no = noVar.get().strip() name = nameVar.get().strip() sex = sexVar.get().strip() age = ageVar.get().strip() if no=="" or name=="": messagebox.showwarning(title = "出错了", message = "学号、姓名不能为空") return with open("d:/students.txt","at") as fsave: stdInfo = ",".join([no, name, sex, age]) stdInfo = stdInfo + "\n" fsave.write(stdInfo) noVar.set("") nameVar.set("") sexVar.set("") ageVar.set("") addButton = tk.Button(window, text = "添加",command = add) addButton.place(x=20, y=190, width=50, height=30)
def query(): qno = noVar.get().strip() # 1、读取学号 # 2、判断 学号是否为空 if qno == "": messagebox.showerror(title = "出错",message="学号不能为空") noVar.set("") return # 3、读取学生信息文件 with open("d:/students.txt","r") as f: students = f.readlines() for student in students: # 44、逐行比对 no, name, sex, age = student.split(",") if no == qno: nameVar.set(name) sexVar.set(sex) ageVar.set(age) break else: messagebox.showerror(title = "出错",message="该学号不存在") queryButton = tk.Button(window, text = "查询", command = query) queryButton.place(x=90, y=190, width=50, height=30)
def clear(): noVar.set("") nameVar.set("") sexVar.set("") ageVar.set("") clearButton = tk.Button(window, text = "清除" , command = clear) clearButton.place(x=160, y=190, width=50, height=30)
def delete(): dno = noVar.get().strip() # 读取学号 if dno == "": messagebox.showerror(title="出错", message="学号不能为空") noVar.set("") return with open("d:/students.txt", "r") as f: lines = f.readlines() with open("d:/students.txt", "w") as f: for line in lines: no, _, _, _ = line.split(",") if no != dno: f.write(line) messagebox.showinfo(title="提示", message="删除成功") noVar.set("") nameVar.set("") sexVar.set("") ageVar.set("") deleteButton = tk.Button(window, text = "删除", command = delete) deleteButton.place(x=230, y=190, width=50, height=30)
window.mainloop(
原文地址: https://www.cveoy.top/t/topic/gj4N 著作权归作者所有。请勿转载和采集!