我将展示减少部分代码,这给了我一个问题。
_tkinter.TclError: image "pyimageN" doesn't exist -N停留1、2、3等的地方…
_tkinter.TclError: image "pyimageN" doesn't exist
第一类使用背景图像显示菜单。
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
这两个类的制作方法类似,所以我不知道为什么第二个类会出错。我确定这不是写错误,例如(用conttruct代替construct),并且我使用的图像确实存在。
所以我认为:
我在概念上犯了一些错误,
或它是python中的错误(或Tkinter的细微行为)。
我解决了自己的问题:
我定义的第二个类是问题,因为它使用了另一个根窗口,别名为Tk()。与普通Tk()窗口等效的是Toplevel(),它与根相同,但没有自己的解释器上下文。
很快,为了解决该问题,我不得不将EditorWindow类的 init ()方法的第一行从
self.Eenu=Tk()
至
self.Eenu=Toplevel()