【问题标题】:Why photoimages don't exist?为什么照片图像不存在?
【发布时间】:2012-10-09 12:25:52
【问题描述】:

我将展示给我带来问题的代码的缩减部分。

_tkinter.TclError: image "pyimageN" doesn't exist - 其中 N 保留 1、2、3 等...

有一个使用背景图像显示菜单的第一类。

class MenuWindow():    #in this class we show the main part of the program
    def __init__(self):
        self.Menu=Tk()
        self.MCanvas=Canvas(self.Menu)
        self.MCanvas.bind("<ButtonPress-1>",self.MenuClick)

        #unuseful lines that configure the window and the canvas#

        self.Background=PhotoImage(height=600,width=700)#a simple tkinter.PhotoImage object

        #other unuseful lines that draw the photoimage ( without reading any file, with the method put())#

        self.MCanvas.create_image((x,y),image=self.Background,state="normal")

        #unuseful lines that continue the drawing of the canvas#

第二个类显示另一个窗口,在背景中使用另一个图像。该类由第一类通过self.MenuClick函数的点击绑定启动。

class EditorWindow():    #in this class we show the main part of the program
    def __init__(self):
        self.Eenu=Tk()
        self.ECanvas=Canvas(self.Eenu)

        #unuseful lines that configure the window and the canvas#

        self.Background=PhotoImage(height=600,width=700)

        #other unuseful lines that draw the photoimage ( without reading any file , with the method put() )#

        self.ECanvas.create_image((x,y),image=self.Background,state="normal")#in this line i get the error

        #unuseful lines that continue the drawing of the canvas#

完整的回溯如下:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1399, in __call__
    return self.func(*args)
  File "/Users/albertoperrella/Desktop/slay.py", line 70, in MenuClick
    EditorWindow(self)
  File "/Users/albertoperrella/Desktop/slay.py", line 85, in __init__
    self.ECanvas.create_image((3,3),image=self.Background,state="normal",anchor="nw")
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 2140, in create_image
    return self._create('image', args, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 2131, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage2" doesn't exist

这两个类的制作方式相似,所以我不知道为什么第二个类会出错。我确信这不是书写错误,例如(构造而不是构造)并且我使用的图像确实存在。

所以我认为:

  • 我犯了一些概念错误,

  • 或者它是python中的一个错误(或Tkinter的微妙行为)。

【问题讨论】:

  • 哪一行引发了错误?你能发布回溯吗?
  • 我认为需要PhotoImage() 的更多细节,因为这似乎是图像的来源。

标签: python image tkinter


【解决方案1】:

我自己解决了这个问题:

我定义的第二个类是问题,因为它使用了另一个根窗口,别名 Tk()。与普通 Tk() 窗口等效的是 Toplevel(),它与根相同,但没有自己的解释器上下文。

简而言之,为了解决这个问题,我不得不将 EditorWindow 类的 init() 方法的第一行从

更改为
        self.Eenu=Tk()

        self.Eenu=Toplevel() 

【讨论】:

  • 正确 - 您不能有两个 Tk 实例。一个实例,并且只调用一次mainloop
猜你喜欢
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2021-12-26
  • 2017-11-05
  • 2015-10-20
  • 2016-07-27
  • 2021-03-20
  • 2012-02-25
相关资源
最近更新 更多