Tkinter pyimage 不存在

问题描述 投票:0回答:6

我知道有很多类似的问题,但没有一个足够简单到我能够理解。我有以下代码:

import Tkinter as tk
from PIL import Image, ImageTk

class MainWindow:
    def __init__(self, master):
        canvas = Canvas(master)
        canvas.pack()
        self.pimage = Image.open(filename)
        self.cimage = ImageTk.PhotoImage(self.pimage)
        self.image = canvas.create_image(0,0,image=self.cimage)


filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()

我收到以下错误:

TclError: image "pyimage36" doesn't exist

我读过一些关于图像对象进行垃圾清理的内容,但我不太明白。

python image tkinter python-imaging-library
6个回答
18
投票

想通了。由于某种原因,在调试器中运行时,如果以前的执行抛出了错误,我会收到“pyimage 不存在”错误。但是,如果我重新启动调试器(或者之前执行的脚本没有抛出错误),则程序运行正常。


4
投票

在使用spyder 3.3.6时,我遇到了同样的错误消息,在收到“Tinker pyimage错误”后,我可以加载和显示.png文件的唯一方法是转到控制台并重新启动内核。之后我工作得很好。


2
投票

(Python 3.8)
如果您使用带有控制台的 IDE(例如 Spyder),只需在控制台中调用

root.mainloop()


很可能您有一堆部分加载的 tkinter GUI,由于阻止
root.mainloop()
函数运行的错误,它们从未成功执行。
运行
root.mainloop()
后,屏幕上可能会出现一堆 GUI。关闭所有这些 GUI 后,尝试再次运行您的代码。
Image of multiple Tkinter GUI's appearing on screen

在此处阅读有关

mainloop()
的更多信息:https://pythonguides.com/python-tkinter-mainloop/


2
投票
from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Title")

img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)

lbl = Label(root, image=bg)
lbl.place(x=0, y=0)

mainloop() 

我遇到了同样的错误。试试这个代码,这会对你有帮助。 另外,如果您创建一个按钮并使用它来打开其他窗口,那么请使用 window = Toplevel(),否则它将再次显示相同的错误。


0
投票

来自程序员

图像“pyimage1”不存在 因为一个程序中只能有一个根窗口,即只能存在一个Tk(),所以其他窗口只能以顶层窗口(Toplevel())的形式存在。

原代码

import tkinter as tk
window = tk.TK()

修改后的代码

import tkinter as tk
window = tk.Toplevel()

其他代码保持不变

https://www.programmersought.com/article/87961175215/


0
投票

在spyder中,更改spyder的设置修复此错误。 更改spyder首选项--> ipython控制台-->图形-->图形后端-->自动

© www.soinside.com 2019 - 2024. All rights reserved.