目的:藉由學號輸入框索引,比對CSV刪除資料,
如沒有就顯示輸入學號錯誤,請重新輸入。
問題一、按下刪除鈕刪除學號"111",CSV內的學號 "111"是有的,但是直接跳過直接顯示輸入 錯誤。
問題二、結束程式後,
每次都會再次儲存writeR.writerow(["學號","姓名","國文","英文","數學"]),
下次新增時,有別的方式檢查,假如有的話就不再疊加嗎?
import tkinter as tk
import tkinter.messagebox
import csv
#新增
def _hit2():
itemList = [enTry1.get(),enTry2.get(),enTry3.get(),enTry4.get(),enTry5.get()]
pttList.append(itemList)
enTry1.delete(0,tk.END)
enTry2.delete(0,tk.END)
enTry3.delete(0,tk.END)
enTry4.delete(0,tk.END)
enTry5.delete(0,tk.END)
enTry1.focus()
enTry2.focus()
enTry3.focus()
enTry4.focus()
enTry5.focus()
#檢視
def _hit3():
csvFile = open("abc.csv","r",newline="",encoding="utf-8-sig")
listFile=csv.reader(csvFile)
for iteM in listFile:
lb.insert(tk.END,iteM)
csvFile.close()
#刪除
def _hit4():
if enTry1.get() in pttList:
del pttList[enTry1.get()]
tk.messagebox.askokcancel("提示", "确定删除"+enTry1.get()+"吗?")
enTry1.delete(0,tk.END)
else:
tk.messagebox.showerror("錯誤", "你的學號錯誤,請重新输入!?")
enTry1.delete(0,tk.END)
#儲存 CSV 資料
def _hit5():
csvFile = open("abc.csv","a",newline="",encoding="utf-8-sig")
writeR=csv.writer(csvFile)
writeR.writerow(["學號","姓名","國文","英文","數學"])
writeR.writerows(pttList)
csvFile.close()
qQ=tk.messagebox.askokcancel("提示","確定要結束程式嗎???")
if qQ:
wiN.destroy()
#介面設定
wiN = tk.Tk()
wiN.title("學校成績系統[測試版]!!!")
wiN.geometry("550x700+500+50")
wiN.resizable(width=False, height=False)
titlE=tk.Label(wiN,text="學校成績系統",font=('Arial',35),width=15,height=1)
titlE.place(x=70,y=15)
#標籤、輸入框設置
global enTry1,enTry2,enTry3,enTry4,enTry5
namE1=tk.Label(wiN,text="學號",font=('Arial',15),bg="yellow",fg="black")
namE1.place(x=100,y=80)
enTry1 = tk.Entry(wiN,font=('Arial',10),bg="Aqua",width=35,bd=4)
enTry1.place(x=150,y=80)
namE2=tk.Label(wiN,text="姓名",font=('Arial',15),bg="yellow",fg="black")
namE2.place(x=100,y=120)
enTry2 = tk.Entry(wiN,font=('Arial',10),bg="Aqua",width=35,bd=4)
enTry2.place(x=150,y=120)
namE3=tk.Label(wiN,text="國文",font=('Arial',15),bg="yellow",fg="black")
namE3.place(x=100,y=160)
enTry3 = tk.Entry(wiN,font=('Arial',10),bg="Aqua",width=35,bd=4)
enTry3.place(x=150,y=160)
namE4=tk.Label(wiN,text="英文",font=('Arial',15),bg="yellow",fg="black")
namE4.place(x=100,y=200)
enTry4 = tk.Entry(wiN,font=('Arial',10),bg="Aqua",width=35,bd=4)
enTry4.place(x=150,y=200)
namE5=tk.Label(wiN,text="數學",font=('Arial',15),bg="yellow",fg="black")
namE5.place(x=100,y=240)
enTry5 = tk.Entry(wiN,font=('Arial',10),bg="Aqua",width=35,bd=4)
enTry5.place(x=150,y=240)
#按鈕設置
global btn1,btn2,btn3,btn4,btn5,lb
btn1 = tk.Button(wiN, text="開啟", font=("Arial", 10), width=5, height=1,fg="black")
btn2 = tk.Button(wiN, text="新增", font=("Arial", 10), width=5, height=1,command=lambda:_hit2(),fg="black")
btn3 = tk.Button(wiN, text="檢視", font=("Arial", 10), width=5, height=1,command=_hit3,fg="black")
btn4 = tk.Button(wiN, text="刪除", font=("Arial", 10), width=5, height=1,command=lambda:_hit4(),fg="black")
btn5 = tk.Button(wiN, text="結束", font=("Arial", 10), width=5, height=1,command=_hit5,fg="black")
btn1.place(x=100,y=280)
btn2.place(x=170,y=280)
btn3.place(x=240,y=280)
btn4.place(x=310,y=280)
btn5.place(x=380,y=280)
#清單設置
sBar=tk.Scrollbar(wiN)
sBar.pack(side=tk.RIGHT,fill=tk.Y)
lb=tk.Listbox(wiN, font=("Arial", 20),yscrollcommand=sBar.set,bg="yellow",width=30)
lb.place(x=50,y=320)
sBar.config(command=lb.yview)
pttList=[]
wiN.mainloop()