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

rbl8hiat  于 2023-06-24  发布在  Windows
关注(0)|答案(6)|浏览(186)

我遇到了一个功能,显示一个图像在屏幕上两秒钟,然后被销毁的麻烦。当程序运行时,函数的初始调用过程工作正常,但如果函数随后通过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.

调用函数的按钮。

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

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

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

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

6ie5vjzr

6ie5vjzr1#

PhotoImage方法为创建的第一个TK()示例创建图像。因此,似乎已经通过替换TopLevel()解决了继承TK()示例的问题。
这可以通过将Tk()示例的master指定为PhotoImage的选项来解决。
我认为这一点应该改变:

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

ebdffaop2#

我也收到了同样的错误,上面的方法可以很好地修复它,但是我也发现,如果你不打算改变所有这些,那么你可以重新启动内核(在jupyter notebook中)或者重新启动你的python解释器
这样下次就能成功了。我不完全确定这是如何工作的,为什么这样做,但它确实是,而且暂时是一个简单的解决方案

pgky5nke

pgky5nke3#

也许不是针对这个确切的案例,但对于类似的案例,我发现习惯

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

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

7vux5j2d

7vux5j2d4#

from tkinter import *
import tkinter as tk
window = tk.Tk()

window.title('Learn')
window.geometry("500x500")

text = Label(window, text = 'label here')
text.pack()

photo = PhotoImage(file='C:\\Users\\user\\Documents\\image\\image_2.png')
labelphoto = Label(window , image = photo)
labelphoto.pack()

window.mainloop()enter code here
z8dt9xmd

z8dt9xmd5#

这是一个代码示例

Root =Tk()
window = Root.Toplevel()

photo =PhotoImage(file='C:\\Users\\user\\Documents\\image\\image_2.png', master= window)

labelphoto = Label(window , image = photo)
labelphoto.pack()

我在photoImage中使用“master”来指定父屏幕。谢谢你😊😊

jckbn6z7

jckbn6z76#

我发现了这个问题,所以我想我会回答自己的任何人谁有这个问题在未来。
当wlcm_scrn按程序运行时,它是当时唯一存在的窗口,因此它可以使用tkinter.tk()。出现错误的原因是调用函数的按钮本身位于一个活动窗口中,该窗口也以Tkinter.tk()的形式运行。因此,当Python/Tkinter试图从按钮构建wlcm_scrn时,它实际上是试图在root下创建两个窗口,然后失败。
解决方案:
换线…

wlcm_scrn = tkinter.Tk()

到这个

wlcm_scrn = tkinter.Toplevel()

...停止错误,图像显示。
我个人将有两个函数的示例。一个在Tk()下调用procedurely,另一个在应用程序内部调用TopLevel()。

相关问题