You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止学生信息系统提交重复ID的学生条目?

学生信息系统ID重复校验实现方案

我正在开发一个学生信息系统(Student Information System),学生信息存储在文本文件中,并通过表格展示。现在需要实现提交时校验学生ID是否已存在,若存在则阻止提交的功能。

现有提交按钮函数

def submit():
    if idEntry.get() == "" or nameEntry.get() == "" or yearEntry.get() == "" or courseEntry.get() == "":
        messagebox.showerror("ERROR", "Student information can't be left in a blank!")
        return
    if not idEntry.get().isdigit():
        messagebox.showerror("ERROR", "Student ID number must be a digit!")
        return
    elif not yearEntry.get().isdigit():
        messagebox.showerror("ERROR", "Student year must be a digit!")
        return
    with open("sis.txt", "a") as f:
        f.write(f"{idEntry.get()},{nameEntry.get()},{yearEntry.get()},{courseEntry.get()}\n")
        f.close
    messagebox.showinfo("Successful", "Student is enrolled")
    student = [idEntry.get(),nameEntry.get(),yearEntry.get(),courseEntry.get()]
    table.insert(parent='',index=0,values=student)

现有表格初始化代码

table = ttk.Treeview(window, columns=('idnum','name','year','course'), show='headings')
table.heading('idnum', text='ID Number')
table.heading('name', text='Name')
table.heading('year', text='Year')
table.heading('course', text='Course')
table.pack(fill='both',expand=True,padx=10,pady=10)

table.bind('<<TreeviewSelect>>', item_select)

with open("sis.txt", "r") as f:
    for line in f:
        data = line.strip().split(",")
        student = (data[0],data[1],data[2],data[3])
        table.insert(parent='',index=0,values=student)

已尝试的无效方案

方案1:

with open("sis.txt", "a") as f:
        for stdnt in table:
            if idEntry.get() == stdnt:
                messagebox.showerror("ERROR", "You can't use the same ID number!")
                return

方案2:

for stndt in table.item():
        if idEntry.get() == stndt:
            messagebox.showerror("ERROR", "You can't use the same ID number!")
        print(stndt)

正确实现方法

要实现ID重复校验,需要遍历已存在的学生ID(从文本文件或表格中获取),对比用户输入的ID,若存在则提示错误并终止提交。

方法1:检查文本文件中的所有ID(推荐)

这种方式能确保校验的是文件中存储的所有历史数据,避免因表格操作导致的数据不一致:

def submit():
    input_id = idEntry.get().strip()
    
    # 保留原有空值与格式校验逻辑
    if input_id == "" or nameEntry.get() == "" or yearEntry.get() == "" or courseEntry.get() == "":
        messagebox.showerror("ERROR", "Student information can't be left in a blank!")
        return
    if not input_id.isdigit():
        messagebox.showerror("ERROR", "Student ID number must be a digit!")
        return
    elif not yearEntry.get().isdigit():
        messagebox.showerror("ERROR", "Student year must be a digit!")
        return
    
    # 新增ID重复校验:读取文件检查所有已存在的ID
    try:
        with open("sis.txt", "r") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                existing_id = line.split(",")[0].strip()
                if existing_id == input_id:
                    messagebox.showerror("ERROR", "You can't use the same ID number!")
                    return
    except FileNotFoundError:
        # 文件不存在时说明是第一个学生,跳过校验
        pass
    
    # 原有提交逻辑
    with open("sis.txt", "a") as f:
        f.write(f"{input_id},{nameEntry.get()},{yearEntry.get()},{courseEntry.get()}\n")
    messagebox.showinfo("Successful", "Student is enrolled")
    student = [input_id, nameEntry.get(), yearEntry.get(), courseEntry.get()]
    table.insert(parent='', index=0, values=student)

方法2:检查表格中已加载的ID

如果追求更快的校验速度,可直接遍历表格中的现有条目:

def submit():
    input_id = idEntry.get().strip()
    
    # 保留原有空值与格式校验逻辑
    if input_id == "" or nameEntry.get() == "" or yearEntry.get() == "" or courseEntry.get() == "":
        messagebox.showerror("ERROR", "Student information can't be left in a blank!")
        return
    if not input_id.isdigit():
        messagebox.showerror("ERROR", "Student ID number must be a digit!")
        return
    elif not yearEntry.get().isdigit():
        messagebox.showerror("ERROR", "Student year must be a digit!")
        return
    
    # 新增ID重复校验:遍历表格所有条目
    for item in table.get_children():
        values = table.item(item)['values']
        if values[0] == input_id:
            messagebox.showerror("ERROR", "You can't use the same ID number!")
            return
    
    # 原有提交逻辑
    with open("sis.txt", "a") as f:
        f.write(f"{input_id},{nameEntry.get()},{yearEntry.get()},{courseEntry.get()}\n")
    messagebox.showinfo("Successful", "Student is enrolled")
    student = [input_id, nameEntry.get(), yearEntry.get(), courseEntry.get()]
    table.insert(parent='', index=0, values=student)

注意点

  • 方法1优先推荐,能保证与文件存储的数据完全一致;
  • 方法2适合表格数据与文件实时同步的场景,校验速度更快;
  • 两种方法都需要在格式校验之后、写入文件之前执行,避免无效写入。

内容的提问来源于stack exchange,提问作者uxzane6225

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 04:05:55