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

时间:2019-01-07 10:58:49

标签: python tkinter global-variables traceback

我有一个程序,它可以非常快速地在屏幕上闪烁红色和蓝色,并且具有停止按钮。当按下停止按钮时,闪烁停止,但是我也遇到了很多相同的错误,它们的运行速度如此之快,以至于大麦可以读取它们。这是错误:

 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是全局变量。有没有一种方法不能使它成为全局变量,并且仍然可以使用?注意:我需要在方法中创建标签,因为这实际上是更大项目的摘要。

1 个答案:

答案 0 :(得分:2)

销毁标签不会停止尝试重新配置该标签的功能。这只会使他们失败。 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()