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
JavaScript
x
34
34
1
def count(main_gif):
2
print("counting..")
3
file = Image.open(main_gif)
4
print("done")
5
return file.n_frames
6
7
frameCount = count(gif)
8
frames = [PhotoImage(file=gif, format='gif -index %i' % i) for i in
9
range(frameCount)] # iterating over the given number of frames
10
11
start = time.perf_counter()
12
13
def update(ind):
14
print(f"updating..{ind}")
15
frame = frames[ind]
16
ind += 1
17
if ind == frameCount:
18
ind = 0
19
label.configure(image=frame)
20
end = time.perf_counter()
21
window.after(100, update, ind)
22
time1 = end - start
23
print(int(time1))
24
if int(time1) == 3:
25
label.destroy()
26
27
print("running..")
28
label = Label(window)
29
print("label made")
30
label.place(x=place_x, y=place_y)
31
print("placed")
32
window.after(0, update, 0)
33
34
and heres the error
JavaScript
1
13
13
1
Traceback (most recent call last):
2
File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
3
return self.func(*args)
4
File "/usr/lib/python3.9/tkinter/__init__.py", line 814, in callit
5
func(*args)
6
File "/home/----/Desktop/Pycharm proj 2/Modules/first(Splash_screen)/Basic/tksplash/tksplash.py", line 83, in update
7
label.configure(image=frame)
8
File "/usr/lib/python3.9/tkinter/__init__.py", line 1646, in configure
9
return self._configure('configure', cnf, kw)
10
File "/usr/lib/python3.9/tkinter/__init__.py", line 1636, in _configure
11
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
12
_tkinter.TclError: invalid command name ".!label"```
13
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:
JavaScript
1
16
16
1
def update(ind):
2
print(f"updating..{ind}")
3
frame = frames[ind]
4
ind += 1
5
if ind == frameCount:
6
ind = 0
7
label.configure(image=frame)
8
end = time.perf_counter()
9
time1 = end - start
10
print(int(time1))
11
if int(time1) == 3:
12
label.destroy()
13
print("label destroyed")
14
else:
15
window.after(100, update, ind)
16