【python学生信息管理系统】调用txt文件,包括登录,对学生信息的增删改查,无需数据库!!亲测可用

总共6个py文件,简单可运行!
用户登录界面
在这里插入图片描述
用户注册界面
在这里插入图片描述
学生管理系统主界面
在这里插入图片描述
添加学生界面
在这里插入图片描述
删除学生界面
在这里插入图片描述
查询学生界面
在这里插入图片描述
修改学生界面
在这里插入图片描述
主程序 students.py

import tkinter as tk
import sys
import tkinter.messagebox
from register import RegisterWindow
from addstudents import AddstudentsWindow
from querystudent import QuerystudentsWindow
from deletestudent import DeletestudentsWindow
from modifystudent import ModifystudentsWindow
# 定义登录窗口类
class LoginWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("用户登录")

        # 设置窗口大小及位置
        width = 240
        height = 150
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")

        # 添加用户名和密码输入框
        tk.Label(self, text="用户名:").grid(row=0, column=0, padx=10, pady=20)
        self.entry_username = tk.Entry(self)
        self.entry_username.grid(row=0, column=1)

        tk.Label(self, text="密码:").grid(row=1, column=0, padx=10, pady=10)
        self.entry_password = tk.Entry(self, show="*")
        self.entry_password.grid(row=1, column=1)

        # 添加登录和注册按钮
        tk.Button(self, text="登录", command=self.login).grid(row=2, column=0, padx=10, pady=10)
        tk.Button(self, text="注册", command=self.register).grid(row=2, column=1, padx=10, pady=10)

    def register(self):
        # 打开注册窗口
        RegisterWindow()


    def login(self):
        username = self.entry_username.get()
        password = self.entry_password.get()

        # 判断用户名和密码是否为空
        if username and password:
            # 打开TXT文档
            file = open("user.txt", "r")

            # 逐行读取内容
            for line in file:
                # 将每一行的内容分割成用户名和密码
                user, pwd = line.strip().split(",")

                # 判断用户名和密码是否匹配
                if username == user and password == pwd:
                    # 关闭TXT文档
                    file.close()

                    # 关闭登录窗口
                    self.destroy()

                    # 创建一个StudentManager对象,进入学生管理系统的界面和功能
                    student_manager = MainWindow()
                    return
            # 关闭TXT文档
            file.close()
            # 弹出一个对话框,显示登录失败的信息
            tk.messagebox.showerror(title="登录失败", message="用户名或密码错误!")

        else:
            # 弹出一个对话框,显示登录失败的信息
            tk.messagebox.showerror(title="登录失败", message="用户名或密码不能为空!")

# 定义主窗口类
class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("学生管理系统")
        self.geometry("600x600")

        # 创建各种组件

        self.button_add = tk.Button(self, text="添加学生", command=self.addstudents)
        self.button_add.grid(row=1, columnspan=2, column=0, padx=30, pady=30)
        self.button_query = tk.Button(self, text="查询学生", command=self.querystudent)
        self.button_query.grid(row=1, columnspan=2, column=2, padx=30, pady=30)
        self.button_delete = tk.Button(self, text="删除学生", command=self.deletestudent)
        self.button_delete.grid(row=2, columnspan=2, column=0, padx=30, pady=30)
        self.button_modify = tk.Button(self, text="修改学生", command=self.modifystudent)
        self.button_modify.grid(row=2, columnspan=2, column=2, padx=30, pady=30)
        self.button_modify = tk.Button(self, text="更新学生信息", command=self.update_students_info)
        self.button_modify.grid(row=3, columnspan=2, column=0, padx=30, pady=30)

        # 新增退出系统按钮
        self.button_exit = tk.Button(self, text="退出系统", command=self.exit_system)
        self.button_exit.grid(row=3, columnspan=2, column=2, padx=30, pady=30)

        # 添加一个方框用来显示students.txt中的数据
        self.frame_data = tk.LabelFrame(self, text="学生信息", width=50, height=50)
        self.frame_data.grid(row=5, column=0, columnspan=4, padx=10, pady=10)

        # 创建一个滚动条
        self.scrollbar = tk.Scrollbar(self.frame_data)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        # 创建一个文本框,用来显示students.txt中的数据
        self.text_data = tk.Text(self.frame_data, yscrollcommand=self.scrollbar.set)
        self.text_data.pack(side=tk.LEFT, fill=tk.BOTH)
        self.scrollbar.config(command=self.text_data.yview)

        # 在文本框中显示students.txt中的数据
        with open("students.txt", "r") as f:
            data = f.read()
            self.text_data.insert(tk.END, data)

        # 在文本框中显示students.txt中的数据
        self.update_students_info() # 调用更新函数


    def update_students_info(self):
        # 清空文本框内容
        self.text_data.delete(1.0, tk.END)
        # 在文本框中显示students.txt中的数据
        with open("students.txt", "r") as f:
            data = f.read()
            self.text_data.insert(tk.END, data)

    # 学生管理函数
    def addstudents(self):
        AddstudentsWindow()

    def querystudent(self):
        QuerystudentsWindow()

    def deletestudent(self):
        DeletestudentsWindow()

    def modifystudent(self):
        ModifystudentsWindow()

    # 退出系统函数
    def exit_system(self):
        # 询问用户是否确定退出
        answer = tk.messagebox.askyesno("退出系统", "您确定要退出系统吗?")
        if answer:
            # 如果确定,关闭主窗口,打开登录窗口
            self.destroy()
            sys.exit()


# 运行登录窗口
if __name__ == '__main__':
    app = LoginWindow()
    app.mainloop()

register.py

import tkinter as tk
# 注册

class RegisterWindow(tk.Toplevel):
    def __init__(self):
        super().__init__()

        self.title("注册新学生信息")

        # 设置窗口大小及位置
        width = 300
        height = 200
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")

        # 添加用户名和密码输入框
        tk.Label(self, text="用户名:").grid(row=0, column=0, padx=10, pady=10)
        self.entry_username = tk.Entry(self)
        self.entry_username.grid(row=0, column=1)

        tk.Label(self, text="密码:").grid(row=1, column=0, padx=10, pady=10)
        self.entry_password = tk.Entry(self, show="*")
        self.entry_password.grid(row=1, column=1)

        # 添加确定和取消按钮
        tk.Button(self, text="确定", command=self.register).grid(row=2, column=0, padx=10, pady=10)
        tk.Button(self, text="取消", command=self.destroy).grid(row=2, column=1, padx=10, pady=10)

    def register(self):
        username = self.entry_username.get()
        password = self.entry_password.get()

        # 判断用户名和密码是否为空
        if username and password:
            # 打开TXT文档
            file = open("user.txt", "a")

            # 将用户名和密码保存到TXT文档中
            file.write(",".join([username, password]) + "\n")
            file.close()

            # 弹出一个对话框,显示注册成功的信息
            tk.messagebox.showinfo(title="注册成功", message="恭喜你,注册成功!")

        else:
            # 弹出一个对话框,显示注册失败的信息
            tk.messagebox.showerror(title="注册失败", message="用户名或密码不能为空!")


        self.destroy()

addstudents.py

import tkinter as tk
# 添加

class AddstudentsWindow(tk.Toplevel):
    def __init__(self):
        super().__init__()

        self.title("添加学生信息")

        # 设置窗口大小及位置
        width = 500
        height = 300
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")


        # 添加用户名和密码输入框
        self.label_stu_id = tk.Label(self, text="学号")
        self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
        self.entry_stu_id = tk.Entry(self)
        self.entry_stu_id.grid(row=0, column=1, padx=10, pady=10)
        self.label_stu_name = tk.Label(self, text="姓名")
        self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
        self.entry_stu_name = tk.Entry(self)
        self.entry_stu_name.grid(row=0, column=3, padx=10, pady=10)

        # 新增性别标签和输入框
        self.label_stu_gender = tk.Label(self, text="性别")
        self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
        self.entry_stu_gender = tk.Entry(self)
        self.entry_stu_gender.grid(row=1, column=3, padx=10, pady=10)
        # 新增专业标签和输入框
        self.label_stu_major = tk.Label(self, text="专业")
        self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
        self.entry_stu_major = tk.Entry(self)
        self.entry_stu_major.grid(row=1, column=1, padx=10, pady=10)
        # 新增班级标签和输入框
        self.label_stu_class = tk.Label(self, text="班级")
        self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
        self.entry_stu_class = tk.Entry(self)
        self.entry_stu_class.grid(row=2, column=1, padx=10, pady=10)

        # 新增成绩标签和输入框
        self.label_stu_score1 = tk.Label(self, text="语文成绩")
        self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
        self.entry_stu_score1 = tk.Entry(self)
        self.entry_stu_score1.grid(row=2, column=3, padx=10, pady=10)

        # 新增成绩标签和输入框
        self.label_stu_score2 = tk.Label(self, text="英语成绩")
        self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
        self.entry_stu_score2 = tk.Entry(self)
        self.entry_stu_score2.grid(row=3, column=1, padx=10, pady=10)

        # 新增成绩标签和输入框
        self.label_stu_score3 = tk.Label(self, text="数学成绩")
        self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
        self.entry_stu_score3 = tk.Entry(self)
        self.entry_stu_score3.grid(row=3, column=3, padx=10, pady=10)

        # 添加确定和取消按钮
        tk.Button(self, text="确定", command=self.addstudents).grid(row=5, column=1, padx=10, pady=10)
        tk.Button(self, text="取消", command=self.destroy).grid(row=5, column=2, padx=10, pady=10)

        # 加载学生数据
        # 学生数据包含性别,专业,班级属性
        self.students = {}
        with open("students.txt", "r") as f:
            for line in f.readlines():
                line = line.strip()
                if not line:
                    continue
                stu_id, stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 = line.split("\t")
                # 学生数据是一个字典,键是学号,值是一个元组,包含姓名,成绩,性别,专业,班级
                self.students[stu_id] = (stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3)



    def addstudents(self):
        stu_id = self.entry_stu_id.get()
        stu_name = self.entry_stu_name.get()
        stu_gender = self.entry_stu_gender.get()
        stu_major = self.entry_stu_major.get()
        stu_class = self.entry_stu_class.get()
        stu_score1 = self.entry_stu_score1.get()
        stu_score2 = self.entry_stu_score2.get()
        stu_score3 = self.entry_stu_score3.get()
        if not stu_id or not stu_name or not stu_score1 or not stu_score2 or not stu_score3 or not stu_gender or not stu_major or not stu_class:
            return
        # 添加学生数据时,需要包含性别,专业,班级
        self.students[stu_id] = (stu_name,  stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3,)
        with open("students.txt", "w") as f:
            for stu_id in sorted(self.students.keys()):
                # 保存学生数据时,需要包含性别,专业,班级
                f.write(
                    f"{stu_id}\t{self.students[stu_id][0]}\t{self.students[stu_id][1]}\t{self.students[stu_id][2]}"
                    f"\t{self.students[stu_id][3]}\t{self.students[stu_id][4]}\t{self.students[stu_id][5]}\t{self.students[stu_id][6]}\n")
        self.entry_stu_id.delete(0, tk.END)
        self.entry_stu_name.delete(0, tk.END)
        self.entry_stu_gender.delete(0, tk.END)
        self.entry_stu_major.delete(0, tk.END)
        self.entry_stu_class.delete(0, tk.END)
        self.entry_stu_score1.delete(0, tk.END)
        self.entry_stu_score2.delete(0, tk.END)
        self.entry_stu_score3.delete(0, tk.END)
        self.entry_stu_score3.delete(0, tk.END)

        tk.messagebox.showinfo("添加成功", "添加学生信息成功,请返回主页面")
        self.destroy()


querystudent.py

import tkinter as tk
# 查询

class QuerystudentsWindow(tk.Toplevel):
    def __init__(self):
        super().__init__()

        self.title("查询学生信息")

        # 设置窗口大小及位置
        width = 500
        height = 300
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")

        # 添加用户名和密码输入框
        self.label_stu_id = tk.Label(self, text="学号")
        self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
        self.entry_stu_id = tk.Entry(self)
        self.entry_stu_id.grid(row=0, column=1, padx=10, pady=10)

        self.label_stu_name = tk.Label(self, text="姓名")
        self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
        self.canvas_stu_name = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_name.grid(row=0, column=3, padx=10, pady=10)
        self.canvas_stu_name.create_rectangle(0, 0, self.canvas_stu_name.winfo_width(), self.canvas_stu_name.winfo_height(),fill='white')

        # 新增性别标签和输入框
        self.label_stu_gender = tk.Label(self, text="性别")
        self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
        self.canvas_stu_gender = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_gender.grid(row=1, column=3, padx=10, pady=10)
        self.canvas_stu_gender.create_rectangle(0, 0, self.canvas_stu_gender.winfo_width(), self.canvas_stu_gender.winfo_height(),fill='white')

        # 新增专业标签和输入框
        self.label_stu_major = tk.Label(self, text="专业")
        self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
        self.canvas_stu_major = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_major.grid(row=1, column=1, padx=10, pady=10)
        self.canvas_stu_major.create_rectangle(0, 0, self.canvas_stu_major.winfo_width(), self.canvas_stu_major.winfo_height(),fill='white')

        # 新增班级标签和输入框
        self.label_stu_class = tk.Label(self, text="班级")
        self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
        self.canvas_stu_class = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_class.grid(row=2, column=1, padx=10, pady=10)
        self.canvas_stu_class.create_rectangle(0, 0, self.canvas_stu_class.winfo_width(), self.canvas_stu_class.winfo_height(),fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score1 = tk.Label(self, text="语文成绩")
        self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
        self.canvas_stu_score1 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score1.grid(row=2, column=3, padx=10, pady=10)
        self.canvas_stu_score1.create_rectangle(0, 0, self.canvas_stu_score1.winfo_width(), self.canvas_stu_score1.winfo_height(),fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score2 = tk.Label(self, text="英语成绩")
        self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
        self.canvas_stu_score2 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score2.grid(row=3, column=1, padx=10, pady=10)
        self.canvas_stu_score2.create_rectangle(0, 0, self.canvas_stu_score2.winfo_width(), self.canvas_stu_score2.winfo_height(),fill='white')


        # 新增成绩标签和输入框
        self.label_stu_score3 = tk.Label(self, text="数学成绩")
        self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
        self.canvas_stu_score3 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score3.grid(row=3, column=3, padx=10, pady=10)
        self.canvas_stu_score3.create_rectangle(0, 0, self.canvas_stu_score3.winfo_width(), self.canvas_stu_score3.winfo_height(),fill='white')


        # 查询确定和取消按钮
        tk.Button(self, text="确定", command=self.querystudents).grid(row=5, column=1, padx=10, pady=10)
        tk.Button(self, text="取消", command=self.destroy).grid(row=5, column=2, padx=10, pady=10)

        # 加载学生数据
        # 学生数据包含性别,专业,班级属性
        self.students = {}
        with open("students.txt", "r") as f:
            for line in f.readlines():
                line = line.strip()
                if not line:
                    continue
                stu_id, stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 = line.split("\t")
                # 学生数据是一个字典,键是学号,值是一个元组,包含姓名,成绩,性别,专业,班级
                self.students[stu_id] = (stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 )

    def querystudents(self):
        stu_id = self.entry_stu_id.get()
        if not stu_id:
            return
        if stu_id in self.students:
            # 查询学生数据时,需要显示性别,专业,班级
            stu_name = self.students[stu_id][0]
            stu_gender = self.students[stu_id][1]
            stu_major = self.students[stu_id][2]
            stu_class = self.students[stu_id][3]
            stu_score1 = self.students[stu_id][4]
            stu_score2 = self.students[stu_id][5]
            stu_score3 = self.students[stu_id][6]
            self.label_stu_id = tk.Label(self, text="学号")
            self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
            self.text_stu_id = tk.Text(self, height=1, width=20)
            self.text_stu_id.grid(row=0, column=1, padx=10, pady=10)
            self.text_stu_id.insert(tk.END, str(stu_id))

            self.label_stu_name = tk.Label(self, text="姓名")
            self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
            self.text_stu_name = tk.Text(self, height=1, width=20)
            self.text_stu_name.grid(row=0, column=3, padx=10, pady=10)
            self.text_stu_name.insert(tk.END, str(stu_name))

            # 新增性别标签和输入框
            self.label_stu_gender = tk.Label(self, text="性别")
            self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
            self.text_stu_gender = tk.Text(self, height=1, width=20)
            self.text_stu_gender.grid(row=1, column=3, padx=10, pady=10)
            self.text_stu_gender.insert(tk.END, str(stu_gender))

            # 新增专业标签和输入框
            self.label_stu_major = tk.Label(self, text="专业")
            self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
            self.text_stu_major = tk.Text(self, height=1, width=20)
            self.text_stu_major.grid(row=1, column=1, padx=10, pady=10)
            self.text_stu_major.insert(tk.END, str(stu_major))

            # 新增班级标签和输入框
            self.label_stu_class = tk.Label(self, text="班级")
            self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
            self.text_stu_class = tk.Text(self, height=1, width=20)
            self.text_stu_class.grid(row=2, column=1, padx=10, pady=10)
            self.text_stu_class.insert(tk.END, str(stu_class))

            # 新增成绩标签和输入框
            self.label_stu_score1 = tk.Label(self, text="语文成绩")
            self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
            self.text_stu_score1 = tk.Text(self, height=1, width=20)
            self.text_stu_score1.grid(row=2, column=3, padx=10, pady=10)
            self.text_stu_score1.insert(tk.END, str(stu_score1))

            # 新增成绩标签和输入框
            self.label_stu_score2 = tk.Label(self, text="英语成绩")
            self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
            self.text_stu_score2 = tk.Text(self, height=1, width=20)
            self.text_stu_score2.grid(row=3, column=1, padx=10, pady=10)
            self.text_stu_score2.insert(tk.END, str(stu_score2))

            # 新增成绩标签和输入框
            self.label_stu_score3 = tk.Label(self, text="数学成绩")
            self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
            self.text_stu_score3 = tk.Text(self, height=1, width=20)
            self.text_stu_score3.grid(row=3, column=3, padx=10, pady=10)
            self.text_stu_score3.insert(tk.END, str(stu_score3))

        else:
            tk.messagebox.showwarning("查询结果", f"学号:{stu_id} 不存在")
        self.entry_stu_id.delete(0, tk.END)


deletestudent.py

import tkinter as tk
# 删除

class DeletestudentsWindow(tk.Toplevel):
    def __init__(self):
        super().__init__()

        self.title("删除学生信息")

        # 设置窗口大小及位置
        width = 500
        height = 300
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")

        # 添加用户名和密码输入框
        self.label_stu_id = tk.Label(self, text="学号")
        self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
        self.entry_stu_id = tk.Entry(self)
        self.entry_stu_id.grid(row=0, column=1, padx=10, pady=10)

        self.label_stu_name = tk.Label(self, text="姓名")
        self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
        self.canvas_stu_name = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_name.grid(row=0, column=3, padx=10, pady=10)
        self.canvas_stu_name.create_rectangle(0, 0, self.canvas_stu_name.winfo_width(),
                                              self.canvas_stu_name.winfo_height(), fill='white')

        # 新增性别标签和输入框
        self.label_stu_gender = tk.Label(self, text="性别")
        self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
        self.canvas_stu_gender = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_gender.grid(row=1, column=3, padx=10, pady=10)
        self.canvas_stu_gender.create_rectangle(0, 0, self.canvas_stu_gender.winfo_width(),
                                                self.canvas_stu_gender.winfo_height(), fill='white')

        # 新增专业标签和输入框
        self.label_stu_major = tk.Label(self, text="专业")
        self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
        self.canvas_stu_major = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_major.grid(row=1, column=1, padx=10, pady=10)
        self.canvas_stu_major.create_rectangle(0, 0, self.canvas_stu_major.winfo_width(),
                                               self.canvas_stu_major.winfo_height(), fill='white')

        # 新增班级标签和输入框
        self.label_stu_class = tk.Label(self, text="班级")
        self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
        self.canvas_stu_class = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_class.grid(row=2, column=1, padx=10, pady=10)
        self.canvas_stu_class.create_rectangle(0, 0, self.canvas_stu_class.winfo_width(),
                                               self.canvas_stu_class.winfo_height(), fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score1 = tk.Label(self, text="语文成绩")
        self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
        self.canvas_stu_score1 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score1.grid(row=2, column=3, padx=10, pady=10)
        self.canvas_stu_score1.create_rectangle(0, 0, self.canvas_stu_score1.winfo_width(),
                                                self.canvas_stu_score1.winfo_height(), fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score2 = tk.Label(self, text="英语成绩")
        self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
        self.canvas_stu_score2 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score2.grid(row=3, column=1, padx=10, pady=10)
        self.canvas_stu_score2.create_rectangle(0, 0, self.canvas_stu_score2.winfo_width(),
                                                self.canvas_stu_score2.winfo_height(), fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score3 = tk.Label(self, text="数学成绩")
        self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
        self.canvas_stu_score3 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score3.grid(row=3, column=3, padx=10, pady=10)
        self.canvas_stu_score3.create_rectangle(0, 0, self.canvas_stu_score3.winfo_width(),
                                                self.canvas_stu_score3.winfo_height(), fill='white')

        # 查询确定和取消按钮
        tk.Button(self, text="确定", command=self.deletestudent).grid(row=5, column=1, padx=10, pady=10)
        tk.Button(self, text="取消", command=self.destroy).grid(row=5, column=2, padx=10, pady=10)

        # 加载学生数据
        # 学生数据包含性别,专业,班级属性
        self.students = {}
        with open("students.txt", "r") as f:
            for line in f.readlines():
                line = line.strip()
                if not line:
                    continue
                stu_id, stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 = line.split("\t")
                # 学生数据是一个字典,键是学号,值是一个元组,包含姓名,成绩,性别,专业,班级
                self.students[stu_id] = (stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 )

    def deletestudent(self):
        stu_id = self.entry_stu_id.get()
        if not stu_id:
            return
        if stu_id in self.students:
            stu_ids = stu_id
            self.students.pop(stu_id)
            with open("students.txt", "w") as f:
                for stu_id in sorted(self.students.keys()):
                    f.write(
                        f"{stu_id}\t{self.students[stu_id][0]}\t{self.students[stu_id][1]}\t{self.students[stu_id][2]}"
                        f"\t{self.students[stu_id][3]}\t{self.students[stu_id][4]}\t{self.students[stu_id][5]}\t{self.students[stu_id][6]}\n")
            tk.messagebox.showinfo("删除结果", f"学号为:{stu_ids} 的同学信息已删除")
        else:
            tk.messagebox.showwarning("删除结果", f" 学号为:{stu_id}的同学信息不存在")
        self.entry_stu_id.delete(0, tk.END)

modifystudent.py

import tkinter as tk
# 修改

class ModifystudentsWindow(tk.Toplevel):
    def __init__(self):
        super().__init__()

        self.title("修改学生信息")

        # 设置窗口大小及位置
        width = 500
        height = 300
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")

        # 添加用户名和密码输入框
        self.label_stu_id = tk.Label(self, text="学号")
        self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
        self.entry_stu_id = tk.Entry(self)
        self.entry_stu_id.grid(row=0, column=1, padx=10, pady=10)

        self.label_stu_name = tk.Label(self, text="姓名")
        self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
        self.canvas_stu_name = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_name.grid(row=0, column=3, padx=10, pady=10)
        self.canvas_stu_name.create_rectangle(0, 0, self.canvas_stu_name.winfo_width(), self.canvas_stu_name.winfo_height(),fill='white')

        # 新增性别标签和输入框
        self.label_stu_gender = tk.Label(self, text="性别")
        self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
        self.canvas_stu_gender = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_gender.grid(row=1, column=3, padx=10, pady=10)
        self.canvas_stu_gender.create_rectangle(0, 0, self.canvas_stu_gender.winfo_width(), self.canvas_stu_gender.winfo_height(),fill='white')

        # 新增专业标签和输入框
        self.label_stu_major = tk.Label(self, text="专业")
        self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
        self.canvas_stu_major = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_major.grid(row=1, column=1, padx=10, pady=10)
        self.canvas_stu_major.create_rectangle(0, 0, self.canvas_stu_major.winfo_width(), self.canvas_stu_major.winfo_height(),fill='white')

        # 新增班级标签和输入框
        self.label_stu_class = tk.Label(self, text="班级")
        self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
        self.canvas_stu_class = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_class.grid(row=2, column=1, padx=10, pady=10)
        self.canvas_stu_class.create_rectangle(0, 0, self.canvas_stu_class.winfo_width(), self.canvas_stu_class.winfo_height(),fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score1 = tk.Label(self, text="语文成绩")
        self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
        self.canvas_stu_score1 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score1.grid(row=2, column=3, padx=10, pady=10)
        self.canvas_stu_score1.create_rectangle(0, 0, self.canvas_stu_score1.winfo_width(), self.canvas_stu_score1.winfo_height(),fill='white')

        # 新增成绩标签和输入框
        self.label_stu_score2 = tk.Label(self, text="英语成绩")
        self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
        self.canvas_stu_score2 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score2.grid(row=3, column=1, padx=10, pady=10)
        self.canvas_stu_score2.create_rectangle(0, 0, self.canvas_stu_score2.winfo_width(), self.canvas_stu_score2.winfo_height(),fill='white')


        # 新增成绩标签和输入框
        self.label_stu_score3 = tk.Label(self, text="数学成绩")
        self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
        self.canvas_stu_score3 = tk.Canvas(self, height=20, width=150, bg='white')
        self.canvas_stu_score3.grid(row=3, column=3, padx=10, pady=10)
        self.canvas_stu_score3.create_rectangle(0, 0, self.canvas_stu_score3.winfo_width(), self.canvas_stu_score3.winfo_height(),fill='white')


        # 查询确定和取消按钮
        tk.Button(self, text="查询", command=self.querystudents).grid(row=4, column=1, padx=10, pady=10)
        tk.Button(self, text="修改", command=self.modifystudent).grid(row=4, column=2, padx=10, pady=10)
        tk.Button(self, text="取消", command=self.destroy).grid(row=4, column=3, padx=10, pady=10)




        # 加载学生数据
        # 学生数据包含性别,专业,班级属性
        self.students = {}
        with open("students.txt", "r") as f:
            for line in f.readlines():
                line = line.strip()
                if not line:
                    continue
                stu_id, stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3 = line.split("\t")
                # 学生数据是一个字典,键是学号,值是一个元组,包含姓名,成绩,性别,专业,班级
                self.students[stu_id] = (stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3)

    def querystudents(self):
        stu_id = self.entry_stu_id.get()
        if not stu_id:
            return
        if stu_id in self.students:
            # 查询学生数据时,需要显示性别,专业,班级
            stu_name = self.students[stu_id][0]
            stu_gender = self.students[stu_id][1]
            stu_major = self.students[stu_id][2]
            stu_class = self.students[stu_id][3]
            stu_score1 = self.students[stu_id][4]
            stu_score2 = self.students[stu_id][5]
            stu_score3 = self.students[stu_id][6]
            self.label_stu_id = tk.Label(self, text="学号")
            self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
            self.text_stu_id = tk.Text(self, height=1, width=20)
            self.text_stu_id.grid(row=0, column=1, padx=10, pady=10)
            self.text_stu_id.insert(tk.END, str(stu_id))


            self.label_stu_name = tk.Label(self, text="姓名")
            self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
            self.text_stu_name = tk.Text(self, height=1, width=20)
            self.text_stu_name.grid(row=0, column=3, padx=10, pady=10)
            self.text_stu_name.insert(tk.END, str(stu_name))


            # 新增性别标签和输入框
            self.label_stu_gender = tk.Label(self, text="性别")
            self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
            self.text_stu_gender = tk.Text(self, height=1, width=20)
            self.text_stu_gender.grid(row=1, column=3, padx=10, pady=10)
            self.text_stu_gender.insert(tk.END, str(stu_gender))


            # 新增专业标签和输入框
            self.label_stu_major = tk.Label(self, text="专业")
            self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
            self.text_stu_major = tk.Text(self, height=1, width=20)
            self.text_stu_major.grid(row=1, column=1, padx=10, pady=10)
            self.text_stu_major.insert(tk.END, str(stu_major))


            # 新增班级标签和输入框
            self.label_stu_class = tk.Label(self, text="班级")
            self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
            self.text_stu_class = tk.Text(self, height=1, width=20)
            self.text_stu_class.grid(row=2, column=1, padx=10, pady=10)
            self.text_stu_class.insert(tk.END, str(stu_class))


            # 新增成绩标签和输入框
            self.label_stu_score1 = tk.Label(self, text="语文成绩")
            self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
            self.text_stu_score1 = tk.Text(self, height=1, width=20)
            self.text_stu_score1.grid(row=2, column=3, padx=10, pady=10)
            self.text_stu_score1.insert(tk.END, str(stu_score1))


            # 新增成绩标签和输入框
            self.label_stu_score2 = tk.Label(self, text="英语成绩")
            self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
            self.text_stu_score2 = tk.Text(self, height=1, width=20)
            self.text_stu_score2.grid(row=3, column=1, padx=10, pady=10)
            self.text_stu_score2.insert(tk.END, str(stu_score2))


            # 新增成绩标签和输入框
            self.label_stu_score3 = tk.Label(self, text="数学成绩")
            self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
            self.text_stu_score3 = tk.Text(self, height=1, width=20)
            self.text_stu_score3.grid(row=3, column=3, padx=10, pady=10)
            self.text_stu_score3.insert(tk.END, str(stu_score3))


        else:
            tk.messagebox.showwarning("查询结果", f"学号:{stu_id} 不存在")
        self.entry_stu_id.delete(0, tk.END)

    def modifystudent(self):

        self.label_stu_id = tk.Label(self, text="学号")
        self.label_stu_id.grid(row=0, column=0, padx=10, pady=10)
        self.entry_stu_id = tk.Entry(self)
        self.entry_stu_id.grid(row=0, column=1, padx=10, pady=10)
        ids = self.text_stu_id.get('1.0', tk.END).strip()
        self.entry_stu_id.insert(0, ids)

        self.label_stu_name = tk.Label(self, text="姓名")
        self.label_stu_name.grid(row=0, column=2, padx=10, pady=10)
        self.entry_stu_name = tk.Entry(self)
        self.entry_stu_name.grid(row=0, column=3, padx=10, pady=10)
        names = self.text_stu_name.get('1.0', tk.END).strip()
        self.entry_stu_name.insert(0, names)

        # 新增性别标签和输入框
        self.label_stu_gender = tk.Label(self, text="性别")
        self.label_stu_gender.grid(row=1, column=2, padx=10, pady=10)
        self.entry_stu_gender = tk.Entry(self)
        self.entry_stu_gender.grid(row=1, column=3, padx=10, pady=10)
        genders = self.text_stu_gender.get('1.0', tk.END).strip()
        self.entry_stu_gender.insert(0, genders)

        # 新增专业标签和输入框
        self.label_stu_major = tk.Label(self, text="专业")
        self.label_stu_major.grid(row=1, column=0, padx=10, pady=10)
        self.entry_stu_major = tk.Entry(self)
        self.entry_stu_major.grid(row=1, column=1, padx=10, pady=10)
        majors = self.text_stu_major.get('1.0', tk.END).strip()
        self.entry_stu_major.insert(0, majors)


        # 新增班级标签和输入框
        self.label_stu_class = tk.Label(self, text="班级")
        self.label_stu_class.grid(row=2, column=0, padx=10, pady=10)
        self.entry_stu_class = tk.Entry(self)
        self.entry_stu_class.grid(row=2, column=1, padx=10, pady=10)
        classs = self.text_stu_class.get('1.0', tk.END).strip()
        self.entry_stu_class.insert(0, classs)

        # 新增成绩标签和输入框
        self.label_stu_score1 = tk.Label(self, text="语文成绩")
        self.label_stu_score1.grid(row=2, column=2, padx=10, pady=10)
        self.entry_stu_score1 = tk.Entry(self)
        self.entry_stu_score1.grid(row=2, column=3, padx=10, pady=10)
        score1s = self.text_stu_score1.get('1.0', tk.END).strip()
        self.entry_stu_score1.insert(0, score1s)

        # 新增成绩标签和输入框
        self.label_stu_score2 = tk.Label(self, text="英语成绩")
        self.label_stu_score2.grid(row=3, column=0, padx=10, pady=10)
        self.entry_stu_score2 = tk.Entry(self)
        self.entry_stu_score2.grid(row=3, column=1, padx=10, pady=10)
        score2s = self.text_stu_score2.get('1.0', tk.END).strip()
        self.entry_stu_score2.insert(0, score2s)

        # 新增成绩标签和输入框
        self.label_stu_score3 = tk.Label(self, text="数学成绩")
        self.label_stu_score3.grid(row=3, column=2, padx=10, pady=10)
        self.entry_stu_score3 = tk.Entry(self)
        self.entry_stu_score3.grid(row=3, column=3, padx=10, pady=10)
        score3s = self.text_stu_score2.get('1.0', tk.END).strip()
        self.entry_stu_score3.insert(0, score3s)

        stu_id = self.entry_stu_id.get()
        stu_name = self.entry_stu_name.get()
        stu_gender = self.entry_stu_gender.get()
        stu_major = self.entry_stu_major.get()
        stu_class = self.entry_stu_class.get()
        stu_score1 = self.entry_stu_score1.get()
        stu_score2 = self.entry_stu_score2.get()
        stu_score3 = self.entry_stu_score3.get()

        if not stu_id or not stu_name or not stu_score1 or not stu_score2 or not stu_score3 or not stu_gender or not stu_major or not stu_class:
            return
        if stu_id in self.students:
            # 修改学生数据时,需要包含性别,专业,班级
            self.students[stu_id] = (stu_name, stu_gender, stu_major, stu_class, stu_score1, stu_score2, stu_score3)
            with open("students.txt", "w") as f:
                for stu_id in sorted(self.students.keys()):
                    f.write(
                        f"{stu_id}\t{self.students[stu_id][0]}\t{self.students[stu_id][1]}\t{self.students[stu_id][2]}"
                        f"\t{self.students[stu_id][3]}\t{self.students[stu_id][4]}\t{self.students[stu_id][5]}\t{self.students[stu_id][6]}\n")
            tk.messagebox.showinfo("修改结果",
                                   f"学号:{stu_id},姓名:{stu_name},性别:{stu_gender},专业:{stu_major},班级:{stu_class},语文成绩:{stu_score1},英语成绩:{stu_score2},数学成绩:{stu_score3}\n")
        else:
            tk.messagebox.showwarning("修改结果", f"学号:{stu_id} 不存在")
        self.entry_stu_id.delete(0, tk.END)
        self.entry_stu_name.delete(0, tk.END)
        self.entry_stu_gender.delete(0, tk.END)
        self.entry_stu_major.delete(0, tk.END)
        self.entry_stu_class.delete(0, tk.END)
        self.entry_stu_score1.delete(0, tk.END)
        self.entry_stu_score2.delete(0, tk.END)
        self.entry_stu_score3.delete(0, tk.END)
        self.entry_stu_score3.delete(0, tk.END)



在同文件夹下创建students.txt和user.txt文件既可运行

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值