I have a program that flashes red and blue on your screen very fast and it has a stop button. When the stop button is pressed the flashing stops but I also get a wall of errors which are all the same, they go so fast I can barley read them. Here is the error:
Traceback (most recent call last):
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\Mihkel\Desktop\epiloop\Assets\example.py", line 27, in re
label2.configure(image=redi)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1482, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1473, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\Mihkel\Desktop\epiloop\Assets\example.py", line 31, in blu
label2.configure(image=bluei)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1482, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1473, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\Mihkel\Desktop\epiloop\Assets\example.py", line 31, in blu
label2.configure(image=bluei)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1482, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\Mihkel\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1473, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"
This is the code:
from tkinter import*
root = Tk()
root.geometry("500x500")
root.title("test")
time_interval = 260
#define images
redi = PhotoImage(file="red.gif")
bluei = PhotoImage(file="blue.gif")
def createimg2():#create the image that will later be reconfigured to make it flash
global label2
label2 = Label(root, image=redi)
label2.image1 = redi
label2.image2 = bluei
label2.pack()
label2.place(x=0, y=0)
createimg2()
#the 4 following methods are what make the image flash
def re():
root.after(time_interval, blu)
label2.configure(image=redi)
def blu():
root.after(time_interval, re)
label2.configure(image=bluei)
def rbgo():
root.after(time_interval, re)
def rb():
re()
blu()
rbgo()
rb()
def kill(): #remove the flashing image
label2.destroy()
btn = Button(root, text="stop", height=2, width=3, command=kill)
btn.pack(pady=100)
root.mainloop()
I believe that the problem is the fact that label1
is a global variable. Is there a way I can not make it a global variable and have it still work? Note: I need the creation of the label to be in a method because this is actually a snippet of a much larger project.
Destroying the label doesn't stop the functions that try to reconfigure that label. It just makes them fail. The kill
function should do something that causes the color-changing functions to stop. For example:
flag = True
def re():
if flag:
root.after(time_interval, blu)
label2.configure(image=redi)
def blu():
if flag:
root.after(time_interval, re)
label2.configure(image=bluei)
def kill(): #remove the flashing image
global flag
flag = False
label2.destroy()