2015-11-05 129 views
-1

此代碼是我正在開發的程序的一部分。它使用CSV從文件中讀取。這部分代碼應該將輸入日期與CSV文件中的日期進行比較,並輸出任何匹配的日期和它們攜帶的信息。這是CSV數據:CSV file 問題是,當我在字段中輸入另一個日期時,我希望它刪除代碼的for循環中創建的任何標籤。 deleteResults函數僅刪除創建的最後一組標籤。有沒有辦法通過另一個函數刪除在for循環中創建的所有標籤,而不是使用for循環創建的標籤數量?謝謝。刪除在For循環中創建的標籤小部件(Tkinter)

#PLANNER 
from tkinter import * 
import tkinter 
from csv import* 
import csv 

colW=['#000000','#f9f4bd','#fff200','black','#2378c8','#ed1c24'] 
colSch=colW 

accessDayPage=False 
results=False 

existingEvents=[] 
eventsFile=open("dayFile.csv","r+") 
evImport=csv.reader(eventsFile) 
for row in evImport: 
    existingEvents.append(row) 


def deleteResults(): 
    global dateLabel,descriptionLabel,titleLabel,results 
    if results==True: 
     dateLabel.destroy() 
     descriptionLabel.destroy() 
     titleLabel.destroy() 
     evTypeLabel.destroy() 
     results=False 
def destroyaccessDay(): 
    global accessDayPage 
    if accessDayPage==True: 
     accessD.destroy() 
     accessM.destroy() 
     accessY.destroy() 
     slash.destroy() 
     slash1.destroy() 
     accessButton.destroy() 

     accessDayPage=False 

def go(): 
    global dayAcc,existingEvents,dateLabel,descriptionLabel,titleLabel,dayAccFORSEARCH,eventsCreated,evTypeLabel,results 
    deleteResults() 
    lineforevent=93 
    leDay="0" 
    leMon="404" 
    if accessD.get()=="01": 
     leDay="1" 
    elif accessD.get()=="02": 
     leDay="2" 
    elif accessD.get()=="03": 
     leDay="3" 
    elif accessD.get()=="04": 
     leDay="4" 
    elif accessD.get()=="05": 
     leDay="5" 
    elif accessD.get()=="06": 
     leDay="6" 
    elif accessD.get()=="07": 
     leDay="7" 
    elif accessD.get()=="08": 
     leDay="8" 
    elif accessD.get()=="09": 
     leDay="9" 
    elif accessD.get()=="1": 
     leDay="1" 
    elif accessD.get()=="2": 
     leDay="2" 
    elif accessD.get()=="3": 
     leDay="3" 
    elif accessD.get()=="4": 
     leDay="4" 
    elif accessD.get()=="5": 
     leDay="5" 
    elif accessD.get()=="6": 
     leDay="6" 
    elif accessD.get()=="7": 
     leDay="7" 
    elif accessD.get()=="8": 
     leDay="8" 
    elif accessD.get()=="9": 
     leDay="9" 
    elif accessD.get()=="10": 
     leDay="10" 
    elif accessD.get()=="11": 
     leDay="11" 
    elif accessD.get()=="12": 
     leDay="12" 
    elif accessD.get()=="13": 
     leDay="13" 
    elif accessD.get()=="14": 
     leDay="14" 
    elif accessD.get()=="15": 
     leDay="15" 
    elif accessD.get()=="16": 
     leDay="16" 
    elif accessD.get()=="17": 
     leDay="17" 
    elif accessD.get()=="18": 
     leDay="18" 
    elif accessD.get()=="19": 
     leDay="19" 
    elif accessD.get()=="20": 
     leDay="20" 
    elif accessD.get()=="21": 
     leDay="21" 
    elif accessD.get()=="22": 
     leDay="22" 
    elif accessD.get()=="23": 
     leDay="23" 
    elif accessD.get()=="24": 
     leDay="24" 
    elif accessD.get()=="25": 
     leDay="25" 
    elif accessD.get()=="26": 
     leDay="26" 
    elif accessD.get()=="27": 
     leDay="27" 
    elif accessD.get()=="28": 
     leDay="28" 
    elif accessD.get()=="29": 
     leDay="29" 
    elif accessD.get()=="30": 
     leDay="30" 
    elif accessD.get()=="31": 
     leDay="31" 

    if accessM.get()=="1": 
     leMon="January" 
    elif accessM.get()=="01": 
     leMon="January" 
    elif accessM.get()=="2": 
     leMon="February" 
    elif accessM.get()=="02": 
     leMon="February" 
    elif accessM.get()=="3": 
     leMon="March" 
    elif accessM.get()=="03": 
     leMon="March" 
    elif accessM.get()=="4": 
     leMon="April" 
    elif accessM.get()=="04": 
     leMon="April" 
    elif accessM.get()=="5": 
     leMon="May" 
    elif accessM.get()=="05": 
     leMon="May" 
    elif accessM.get()=="6": 
     leMon="June" 
    elif accessM.get()=="06": 
     leMon="June" 
    elif accessM.get()=="7": 
     leMon="July" 
    elif accessM.get()=="07": 
     leMon="July" 
    elif accessM.get()=="8": 
     leMon="August" 
    elif accessM.get()=="08": 
     leMon="August" 
    elif accessM.get()=="9": 
     leMon="September" 
    elif accessM.get()=="09": 
     leMon="September" 
    elif accessM.get()=="10": 
     leMon="October" 
    elif accessM.get()=="10": 
     leMon="October" 
    elif accessM.get()=="11": 
     leMon="November" 
    elif accessM.get()=="11": 
     leMon="November" 
    elif accessM.get()=="12": 
     leMon="December" 
    elif accessM.get()=="12": 
     leMon="December" 
    dayAcc=str(leDay+leMon+accessY.get()) 
    dayAccFORSEARCH=str(dayAcc+"'") 



    for i in existingEvents: 
     global results 
     if i[0]==dayAccFORSEARCH:    
      deleteResults() 

      titleLabel=tkinter.Label(main, text=i[1],fg=colSch[3],bg=colSch[1],font=("DFKai-SB",9,"bold")) 
      titleLabel.place(x=140,y=lineforevent) 
      evTypeLabel=tkinter.Label(main, text=i[2],fg=colSch[4],bg=colSch[0],font=("Consolas",9)) 
      evTypeLabel.place(x=320,y=lineforevent) 
      descriptionLabel=tkinter.Label(main,text=i[3],fg=colSch[5],bg=colSch[1],font=("Garamond",9)) 
      descriptionLabel.place(x=420,y=lineforevent) 
      lineforevent+=35 

    dateLabel=tkinter.Label(main, text=dayAcc,fg=colSch[4],bg=colSch[0],font=("Consolas",12)) 
    dateLabel.place(x=140,y=35) 
    results=True 



def accessDay(): 

    destroyaccessDay() 
    deleteResults() 

    global accessD,accessM,accessY,slash,slash1,accessButton,accessDayPage 

    accessD=tkinter.Entry(main,width=2) 
    accessD.place(x=140, y=8) 
    slash=tkinter.Label(main,text="/",fg=colSch[4],bg=colSch[0],font=("14")) 
    slash.place(x=160,y=8) 
    accessM=tkinter.Entry(main,width=2) 
    accessM.place(x=175, y=8) 
    slash1=tkinter.Label(main,text="/",fg=colSch[4],bg=colSch[0],font=("14")) 
    slash1.place(x=195,y=8) 
    accessY=tkinter.Entry(main,width=4) 
    accessY.place(x=210, y=8) 
    accessButton=tkinter.Button(main, text="Go",bg=colSch[2],fg=colSch[4],width=6,height=1, font=("Copperplate Gothic Light","9"),command=go)## 
    accessButton.place(x=245,y=8) 

    accessDayPage=True 




main=tkinter.Tk() 
main.configure(bg=colSch[0]) 
main.geometry('780x375') 
w=Canvas(main,width=780,height=375) 
w.pack() 
rect1=w.create_rectangle(0,0,775,375,fill=colSch[0]) 
rect2=w.create_rectangle(0,0,120,375,fill=colSch[1]) 

accessDay=tkinter.Button(main,text="Access Day",bg=colSch[2],fg=colSch[4],width=15,height=3,relief=FLAT, font=("Bernard MT Condensed","11"),command=accessDay) 
accessDay.place(x=5,y=5) 





main.mainloop() 

回答

0

你會發現,該方法類似於這個帖子here,我給的例子使用動態地生成小部件。你會想看看.destoy()代替.configure()

旁註:

我會建議使用in您的if語句,因爲要爲不同的實例執行相同的操作縮短了代碼。

if accessM.get() in {"1", "01"}: 
    leMon="January" 

或者如Bryan Oakley的建議。使用字典。我相信它會是這樣的。

date_values = {"01" : ("January", range(1, 31)), 
       "02" : ("February", range(1, 30)), # Check for leap years 
       .....} 

leMon = date_values[accessM.get()][0] 
d = int(accessD.get()) 
# Checks if day is in month range 
leDay = str(d) if d in date_values[accessM.get()][1] else None 
+0

甚至比in更好的是創建一個字典或列表,並使用輸入字段的值來查找該項目。這24行代碼可以減少到三個或四個。 –

+0

沒錯,忘了。這是如何完成的? –

+0

謝謝你的答案。我只用了大約半年的python,所以我不知道字典的存在。謝謝您的幫助。 – FrankLo