Skip to content
Advertisement

deleting a label after a given time period

I am trying to delete a label after a given time period but I am getting an error which I understand

here’s the code which is giving the error

    def count(main_gif):
        print("counting..")
        file = Image.open(main_gif)
        print("done")
        return file.n_frames

    frameCount = count(gif)
    frames = [PhotoImage(file=gif, format='gif -index %i' % i) for i in
              range(frameCount)]  # iterating over the given number of frames

    start = time.perf_counter()

    def update(ind):
        print(f"updating..{ind}")
        frame = frames[ind]
        ind += 1
        if ind == frameCount:
            ind = 0
        label.configure(image=frame)
        end = time.perf_counter()
        window.after(100, update, ind)
        time1 = end - start
        print(int(time1))
        if int(time1) == 3:
            label.destroy()

    print("running..")
    label = Label(window)
    print("label made")
    label.place(x=place_x, y=place_y)
    print("placed")
    window.after(0, update, 0)

and heres the error

Traceback (most recent call last):
  File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/usr/lib/python3.9/tkinter/__init__.py", line 814, in callit
    func(*args)
  File "/home/----/Desktop/Pycharm proj 2/Modules/first(Splash_screen)/Basic/tksplash/tksplash.py", line 83, in update
    label.configure(image=frame)
  File "/usr/lib/python3.9/tkinter/__init__.py", line 1646, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python3.9/tkinter/__init__.py", line 1636, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"```

how can I delete the label ??

Advertisement

Answer

As the error said, the label has already been destroyed when update() is triggered/executed by .after(). So you should not schedule to call update() after the label is destroyed:

def update(ind):
    print(f"updating..{ind}")
    frame = frames[ind]
    ind += 1
    if ind == frameCount:
        ind = 0
    label.configure(image=frame)
    end = time.perf_counter()
    time1 = end - start
    print(int(time1))
    if int(time1) == 3:
        label.destroy()
        print("label destroyed")
    else:
        window.after(100, update, ind)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement