Python tkinter.TclError:图像“pyimage3”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20251161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:00:51  来源:igfitidea点击:

tkinter.TclError: image "pyimage3" doesn't exist

pythonwindowspython-3.xtkinter

提问by I_do_python

I'm having trouble with a function that shows an image for two seconds on screen, and then is destroyed. When the program runs the functions initial call procedurely works fine, but if the function is then called via a button built in tkinter I get an error.

我遇到了在屏幕上显示图像两秒钟然后被破坏的功能的问题。当程序运行时,函数初始调用程序正常工作,但是如果然后通过 tkinter 中内置的按钮调用该函数,我会收到错误消息。

appcwd = os.getcwd()
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
size = str(screensize[0])+'x'+str(screensize[1])

def wlcm_scrn(event=None):
    def destroy_wlcm(event=None):
        wlcm_scrn.destroy()
    global appcwd
    global screensize
    wlcm_scrn = tkinter.Tk()
    file=appcwd+"\Run_Files\splash.gif"
    splsh_img = tkinter.PhotoImage(file=file) 
    splosh = tkinter.Label(wlcm_scrn,image=splsh_img)
    wlcmh = splsh_img.height()/2
    wlcmw = splsh_img.width()/2
    splosh.pack()
    wlcm_scrn.config(bg='black')
    wlcm_scrn.overrideredirect(True)
    wlcm_scrn.bind("<Escape>",destroy_wlcm)
    wlxym = '+'+str(int((screensize[0]/2)-wlcmw))+'+'+str(int((screensize[1]/2)-wlcmh))
    wlcm_scrn.geometry(wlxym)
    wlcm_scrn.wm_attributes("-topmost", 1)
    wlcm_scrn.after(2000,destroy_wlcm)
    wlcm_scrn.mainloop()

wlcm_scrn() #Call through procedure.

Button that calls the function.

调用函数的按钮。

view_img = tkinter.Button(cfrm,text='Show splash image',command=wlcm_scrn)

Error message when called through button command.

通过按钮命令调用时的错误消息。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:\Python33\POS_Solution\Rattle_Hum_POS.py", line 1755, in run_wlcm_scrn
    wlcm_scrn()
  File "C:\Python33\POS_Solution\Rattle_Hum_POS.py", line 34, in wlcm_scrn
    splosh = tkinter.Label(wlcm_scrn,image=splsh_img)
  File "C:\Python33\lib\tkinter\__init__.py", line 2596, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 2075, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage3" doesn't exist

What is "pyimage3" and why doesn't it exist? Any help would be apprecaited. Thanks.

什么是“pyimage3”,为什么它不存在?任何帮助将不胜感激。谢谢。

采纳答案by I_do_python

I found the issue so figured I'd answer myself for anyone who has this issue in the future.

我发现了这个问题,所以我想我会为将来遇到这个问题的任何人回答自己。

When the wlcm_scrn runs procedurely it is the only window that exists at that point in time, and so it can use tkinter.Tk(). The error arises because the button that calls the function is itself sitting in an active window that is also running as Tkinter.Tk(). So when Python/Tkinter tries to build wlcm_scrn from the button, it's essentially trying to create two windows under root and falling over.

当 wlcm_scrn 程序运行时,它是当时唯一存在的窗口,因此它可以使用 tkinter.Tk()。出现错误是因为调用该函数的按钮本身位于一个活动窗口中,该窗口也作为 Tkinter.Tk() 运行。因此,当 Python/Tkinter 尝试从按钮构建 wlcm_scrn 时,它本质上是尝试在 root 下创建两个窗口并摔倒。

The solution:

解决方案:

Changing line...

换线...

wlcm_scrn = tkinter.Tk()

to this...

到这...

wlcm_scrn = tkinter.Toplevel()

...stops the error, and the image shows.

...停止错误,并显示图像。

I personally am going to have two instances of the function. One called procedurely under Tk(), and one called within the application under TopLevel().

我个人将拥有该函数的两个实例。一种在 Tk() 下按程序调用,另一种在 TopLevel() 下的应用程序内调用。

回答by SeF

Maybe not for this exact case, but for a similar one, I found that the customary

也许不是对于这种确切的情况,而是对于类似的情况,我发现习惯上

if __name__ == '__main__':
    wlcm_scrn()  #Call through procedure.

solves the problem as well. This seems to kill the active window and to create a new one each time you re-call the same module.

也解决了这个问题。这似乎会杀死活动窗口并在每次重新调用同一个模块时创建一个新窗口。

回答by Takahiro Funahashi

The PhotoImage method creates an image for the first TK () instance created. Thus, it seems to have solved to inherit TK () instance by replacing TopLevel ().

PhotoImage 方法为创建的第一个 TK () 实例创建一个图像。至此,似乎已经解决了通过替换TopLevel()来继承TK()实例。

This can be solved by specifying the master of the Tk () instance as the option of PhotoImage.

这可以通过将 Tk() 实例的 master 指定为 PhotoImage 的选项来解决。

I think this should be changed.:

我认为这应该改变。:

splsh_img = tkinter.PhotoImage(file=file,master=wlcm_scrn)