4

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

_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 的微妙行为)。

4

1 回答 1

6

我解决了自己的问题:

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

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

        self.Eenu=Tk()

        self.Eenu=Toplevel() 
于 2012-10-09T14:10:53.973 回答