delete text from window

Viewed 49

I'm trying to build a frame showing time/day/date and photos. the lower frame contain photo and what is next as a text on the right side. at a certain time, the photo changes and the text changed to a countdown counter. at the end of the timer the main photo return back and the text showing what is next. when i run the code I still see the remaining text and I can't destroy it using destroy or grid_forget

from tkinter import *
import time



labellist=['y1','y2','y3','y4','y5','y6', ..., 'y9']
lbl=1

root = Tk()


################uper frame for Day---Date----Time#####
frameup = Frame(root, highlightthickness=4, relief='solid', width=0, height=70)
frameup.grid(row=0, column=0, columnspan=2,  padx=5, pady=5)
frameup.config(highlightbackground='red3')

################lower frame############
framelow = Frame(root, highlightthickness=4, relief='solid', width=1, height=250)
framelow.grid(row=1, column=0, padx=180)
framelow.config(highlightbackground='red3')

################frame Left for the photo#############
frameL = Frame(framelow, width=0, height=0, relief='ridge', highlightthickness=1)
frameL.grid(row=0, column=0, padx=1)
frameL.config()

###############frame Right for Time Duration Countdown OR Next########
frameR = Frame(framelow)
frameR.grid(row=0, column=1, padx=4)
frameR.config(highlightbackground='green')
nexttext = Label(frameR, text=('Next''\n'+(labellist[lbl])), font='infra 45 bold')
nexttext.grid(row=0, column=0)

labelTD = Label(frameup)
labelTD.grid(row=0, column=0)

x1=PhotoImage(file='file path.png')
x2=PhotoImage(file='file path.png')
x3=PhotoImage(file='file path.png')
x4=PhotoImage(file='file path.png')
x5=PhotoImage(file='file path.png')
x6=PhotoImage(file='file path.png')
x7=PhotoImage(file='file path.png')
x8=PhotoImage(file='file path.png')
x9=PhotoImage(file='file path.png')
x10=PhotoImage(file='file path.png')

imageList=[x1,x2,x3,x4.....,x10
photo_prog = Label(frameL, image=imageList[img])
#
def countdown(n):
    remainingtext = Label(frameR, text=' Time Remaining: ', font='infra 45 bold')
    remainingtext.grid(row=0, column=0)

######this part is to adjust the delay######
    if Hor == 5:
        n = 7080
    if Hor == 6:
        n = 3480

    if n == 1700 or n== 900:
        n= n -10
    if n== 5200 or n==3500:
        n= n -10
######################################

    mn, secs = divmod(n, 60)
    hr, mn = divmod(mn, 60)

    labelCD.config(text=f"{hr:02}:{mn:02}:{secs:02}")
    labelCD.grid(row=1, column=0)
    labelCD.config(font='infra 55 bold', foreground='black', background='white')

    if n >= 0:
        labelCD.after(1000, countdown, n - 1)

    else:
        remainingtext.grid_forget()
        #remainingtext.destroy()
########the next 2 line3 is to cover the remaining time with a blank ###########
        # remainingtext = Label(frameR, text='                                       ', font='infra 45 bold')
    
        photo_prog.destroy()
        labelCD.destroy()
        StudioC = Label(frameL, image=photoGenral)
        StudioC.grid(row=0, column=0)
        nexttext = Label(frameR, text=('Next''\n'+(labellist[lbl])), font='infra 45 bold')
        nexttext.grid(row=1, column=0)


########## Showing Day / Date / Time ############
def clock():
    t = time.strftime('%A''\t''%D''\t''%H:%M:%S', time.localtime()).upper()
    global lbl
    if t != '':
        labelTD.config(text=t, font='infra 55 bold', foreground='red3', background='white')
        global Hor
        Hr = time.strftime('%H')
        Mn = time.strftime('%M')
        Sc = time.strftime('%S')
        Dy = time.strftime('%A')
        Hor= int(Hr)
########### next part is the time when i want to photo change
        if int(Hr) == 5 and int(Mn) == 00 and int(Sc) == 0 and (
                Dy == 'Saturday' or 'Sunday'):
            StudioC.destroy()
            ##replace with:## progimage=Label(image=imageList[0])
            ##replace with ##progimage.grid(row=0,column=0)
            countdown(7020)  ######## here you can set the duration for each show

        if int(Hr) == 4 and int(Mn) == 30 and int(Sc) == 0:  ####just to trigger the timer###
            lbl=1
            nexttext.destroy()
            StudioC.destroy()

            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(8800)  ######## here you can set the duration for each show

        if int(Hr)==7 and int(Mn)==26 and int(Sc)==0:
            lbl=1
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(130)  ######## here you can set the duration for each show

        if int(Hr)==7 and int(Mn)==56 and int(Sc)==0:
            lbl=1
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(130)  ######## here you can set the duration for each show


        if int(Hr)==8 and int(Mn)==27 and int(Sc)==30:
            lbl = 1
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(130)  ######## here you can set the duration for each show

        if int(Hr)==8 and int(Mn)==56 and int(Sc)==30:
            lbl=2
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(130)  ######## here you can set the duration for each show

        if int(Hr) == 12 and int(Mn) == 0 and int(Sc) == 0:  ####just to trigger the timer###
            lbl=3
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(1680)  ######## here you can set the duration for each show

        if int(Hr) == 16 and int(Mn) == 0 and int(Sc) == 0:  ####just to trigger the timer###
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(3540)  ######## here you can set the duration for each show

        if int(Hr) == 17 and int(Mn) == 0 and int(Sc) == 0:  ####just to trigger the timer###
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(3540)  ######## here you can set the duration for each show

        if int(Hr) == 18 and int(Mn) == 0 and int(Sc) == 0:
            lbl=4
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(1680)  ######## here you can set the duration for each show

        if int(Hr) == 19 and int(Mn) == 0 and int(Sc) == 0:
            lbl=5
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(1680)  ######## here you can set the duration for each show

        if int(Hr) == 22 and int(Mn) == 0 and int(Sc) == 0:
            lbl=6
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(3540)  ######## here you can set the duration for each show


        if int(Hr) == 23 and int(Mn) == 0 and int(Sc) == 0:
            lbl = 0
            nexttext.destroy()
            StudioC.destroy()
            photo_prog = Label(frameL, image=imageList[1])
            photo_prog.grid(row=0, column=0)
            countdown(1680)  ######## here you can set the duration for each show

        labelTD.after(1000, clock)

labelCD = Label(frameR)
labelCD.grid(row=1, column=0)

StudioC = Label(frameL, image=imageList[10])
StudioC.grid()


clock()
root.geometry('2160x400')

root.mainloop()
2 Answers

I added the remaining time text to the LCD and added \n between them it works because it removes the LCD widget properly but not that text. thanks for your help, nothing seems work maybe something wrong with building the frame,sorry i'm new here and trying to learn, so that was a good walk around :)

As @acw1668 said, the remainingtext is instantiated every time the countdown() function is called. It creates multiple instances of the remainingtext and thus, only deletes the one of the top and the other instances are not deleted.

ANSWER HERE

I strongly suggest taking out the remainingtext and all the other Tkinter objects that you want to delete after the countdown() function finishes, and typing in outside the countdown() function so it is only instantiated once.

# making an instance of the remaining text object
remainingtext = Label(frameR, text=' Time Remaining: ', font='infra 45 bold')
remainingtext.grid(row=0, column=0)

def countdown(n):

    # referring it

    global remainingtext

    if Hor == 5:
        n = 7080
    if Hor == 6:
        n = 3480

    if n == 1700 or n== 900:
        n= n -10
    if n== 5200 or n==3500:
        n= n -10
######################################

    mn, secs = divmod(n, 60)
    hr, mn = divmod(mn, 60)

    labelCD.config(text=f"{hr:02}:{mn:02}:{secs:02}")
    labelCD.grid(row=1, column=0)
    labelCD.config(font='infra 55 bold', foreground='black', background='white')

    if n >= 0:
        labelCD.after(1000, countdown, n - 1)

    else:
        remainingtext.destroy()

        photo_prog.destroy()
        labelCD.destroy()
        StudioC = Label(frameL)
        StudioC.grid(row=0, column=0)
        nexttext = Label(frameR, text=('Next''\n'+(labellist[lbl])), font='infra 45 bold')
        nexttext.grid(row=1, column=0)

I don't have the exact files or anything like that so I cant run the code but I am pretty sure this will work.

(BTW please organize your code with "#" and not "###################")

(sorry for my bad spellings.)

Related