2012-10-09 31 views
2

我將顯示減少的部分代碼,這給我一個問題。爲什麼python photoimages不存在?

「_tkinter.TclError:圖像‘pyimageN’不存在」 - 其中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# 

的complere回溯如下:

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 

這兩個類都以類似的方式進行,所以我不知道爲什麼我的錯誤與第二個。我相信,這不是一個寫錯誤,例如(重構而不是構造),並且我正在使用的圖像實際上存在。

所以我認爲:

  • 我提出一些概念錯誤,

  • ,或者是(或微妙的行爲的Tkinter的)在python

+0

哪一行引發錯誤?你可以發佈回溯? – mgilson

+0

我覺得需要更多'PhotoImage()'的細節,因爲這似乎是圖像的來源。 – unwind

回答

5

我的錯誤解決了自己的問題:

我定義的第二個類是問題導致它使用了非她的根窗口,別名Tk()。相當於正常的Tk()窗口是Toplevel(),它與根相同,但沒有自己的解釋器上下文。

不久,爲了解決問題,我不得不把EditorWindow類的初始化()方法的最前一頁線從

 self.Eenu=Tk() 

更改爲

 self.Eenu=Toplevel() 
+0

正確 - 你不能有兩個'Tk'的實例。一個實例,並且只調用一次'mainloop'。 –