问题描述
我将显示代码的缩小部分,这给我一个问题。
I will show a reduced portion of the code that gives me a problem.
_ tkinter.TclError:imagepyimageN不存在 - 其中N保留1或2或3等...
"_tkinter.TclError: image "pyimageN" doesn't exist" - where N stays for 1 or 2 or 3 etc...
有一个第一堂课显示在后台使用图片的菜单。
There is a first class that shows a menu using an image in the background.
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的点击绑定启动的。
And a second class that shows another window, using another image in the background. This class is launched by the first class via click binding of the function 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
这两个类是以类似的方式制作的,所以我不知道为什么我会在第二个类中得到错误。我确信这不是写错误,例如(构造而不是构造),并且我正在使用的图像实际存在。
The two classes are made in a similar way, so I don't know why I get the error with the second one. I am sure that it isn't a writing error e.g.(conttruct instead of construct) and that the images I am using actually exist.
所以我认为:
-
我犯了一些概念错误,
I am making some concept mistakes,
或者它是python中的错误(或TkInter的微妙行为)
or it is a bug (or subtle behaviour of TkInter) in python
推荐答案
我解决了我自己的问题:
I solved myself the problem :
我定义的第二个类是问题因为它使用了另一个根窗口,别名Tk()。相当于普通的Tk()窗口是Toplevel()与root相同但没有自己的解释器上下文。
the second class i defined was the problem cause it used another root window , alias Tk() .an equivalent to the normal Tk() window is the Toplevel() that is the same as a root but hasn't its own interpreter context.
很快,为了解决问题,我必须改变fisrt的编辑器窗口的 init ()方法
shortly , to solve the problem i had to change the fisrt line of the init() method of the EditorWindow class from
self.Eenu=Tk()
到
self.Eenu=Toplevel()
这篇关于为什么python photoimages不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!