销毁 Tkinter 图像小部件后的回溯错误 (Python)

Traceback Errors After Destroying a Tkinter Image Widget (Python)

我有一个程序可以在你的屏幕上快速闪烁红色和蓝色,它有一个停止按钮。当按下停止按钮时,闪烁停止了,但我也得到了一堆错误,它们都是一样的,它们走得太快了,我可以大麦读取它们。这是错误:

 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"

这是代码:

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

我认为问题在于 label1 是一个全局变量。有没有办法让它成为一个全局变量并让它仍然有效?注意:我需要在方法中创建标签,因为这实际上是一个更大项目的片段。

销毁标签不会停止尝试重新配置该标签的函数。这只会让他们失败。 kill 函数应该做一些导致变色函数停止的事情。例如:

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