删除在For循环(Tkinter)中创建的Label小部件

时间:2015-11-05 14:30:01

标签: python loops for-loop tkinter labels

此代码是我正在开发的程序的一部分。它使用CSV从文件中读取。这部分代码应该将输入日期与CSV文件中的日期进行比较,并输出任何匹配日期及其携带的信息。这是CSV数据:CSV file 问题是,当我在字段中输入另一个日期时,我希望它删除在代码的for循环中创建的任何标签。 deleteResults函数仅删除最后创建的标签集。有没有办法通过另一个函数删除for循环中创建的所有标签,无论使用for循环创建的标签数量是多少?感谢。

#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()

1 个答案:

答案 0 :(得分:0)

您会发现该方法与此帖here类似,我给出的示例使用动态生成的小部件。您需要查看.destoy()而不是.configure()

旁注:

我建议使用in来缩短代码,以便缩短代码,因为您正在为不同的实例执行相同的操作。

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

另外,正如布莱恩奥克利建议的那样。使用字典。我相信它会是这样的。

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