2014-10-31 133 views
0

我在一个单独的类中显示画布图像时遇到了一些问题。我的Tkinter帆布图像在哪里?

我有一个顶级应用程序,另一个顶级类代表一种面板,主窗口画布的小图像用于导航目的。

我知道我必须保留一个对我的画布图像的引用,以便它可以从垃圾回收中安全,但是,我无法达到预期的效果。

为面板类的代码如下:

from PIL import Image, ImageTk 

class MultiPanel(Toplevel): 

    def __init__(self,parent,cookie): 
     Toplevel.__init__(self,parent) 
     self.parent=parent 
     self.transient(self.parent) 
     self.attributes('-topmost',True) 
     self.protocol("WM_DELETE_WINDOW",self._NULL) 
     self.visible=[BooleanVar()] * 2 
     for i in range(0,1): self.visible[i].set(True) 
     filename=cookie['basename']+os.sep+cookie['current_file'] 
     self.PANEL=PanedWindow(self,orient=VERTICAL) 
     self.PANEL.pack(fill=BOTH,expand=1) 

     self.nFrame=Frame(self.PANEL) 
     self.Navi=NaviPanel(self.nFrame, imgpth=filename) 
     self.image=self.Navi.image 
     self.Navi.pack() 

     self.bFrame=Frame(self.nFrame) 
     bToggle=Checkbutton(self.bFrame,text='Info panel', variable=self.visible[1],onvalue=True,offvalue=False,command=lambda i=0:self.toggle_vis(index=i)) 
     bToggle.pack(side=RIGHT) 
     self.bFrame.pack(side=RIGHT) 

     self.bottom=[] 
     self.bottom.append(Frame(self.PANEL)) 
     l=Label(self.bottom[0], text="bottom pane").pack() 

     self.PANEL.add(self.nFrame) 
     self.PANEL.add(self.bottom[0]) 

    def _NULL(self): 
     pass 

    def toggle_vis(self,index): 
     if not self.visible[index+1].get(): self.PANEL.forget(self.PANEL.panes()[index+1]) 
     else: self.PANEL.add(self.bottom[index]) 

class NaviPanel(Frame): 
    def __init__(self,parent,imgpth): 
     Frame.__init__(self,parent) 
     self.image=ImageTk.PhotoImage(Image.open(os.path.normpath(imgpth))) 
     self.parent=parent 
     self.canvas=Canvas(self) 
     self.canvas.pack(expand=YES,fill=BOTH) 
     self.canvas.create_image(0,0,image=self.image,anchor="ne") 

    def redraw(self,x,y,cextent,pImage,scale,col,fname): 
     self.canvas.create_image(0,0,image=pImage,anchor="nw",tags=("bg",0)) #in the panel 
     self.canvas.configure(width=x,height=y) 
     self.filename_label.configure(text=fname) 
     rect = [int(float(i)/scale) for i in cextent] #rectangle coordinates 
     self.canvas.create_rectangle(rect,outline=col,tags=("view",0)) #in the panel 

    def moveview(self,x,y,cextent,scale,col): 
     self.canvas.delete('view') #destroy rectangle 
     rect = [int(float(i)/scale) for i in cextent] #rectangle coordinates 
     self.canvas.create_rectangle(rect,outline=col,tags=("view",0)) #in the panel 

    def hide(self): 
     self.withdraw() 

    def show(self): 
     self.deiconify() 

    def toggle(self,Event=None): 
     self.visible = not self.visible 
     if self.visible: 
      self.hide() 
     else: 
      self.show() 

    def _NULL(self): 
     pass 

在我的主要应用,我被

 self.panel=MultiPanel(self,cookie=self.cookie,ptitle=PTITLE) 
     self.pImage=self.panel.image 

我试图引用实际图像的所有方式创建窗口从主应用程序到画布frame元素,以及从主应用程序引用整个画布。

我的错误在哪里?

PS:对不起,很长的文章!

回答

0

我深表歉意,但是错误出现在我用于画布图像的错误锚点选项中。用anchor ='nw'代替它解决了所有问题。看到一个与没有参照图像的问题非常相似的行为是非常令人困惑的,所以我迷失了方向!