[英]Where is my Tkinter Canvas image?
我在单独的类中显示画布图像时遇到一些问题。
我有一个顶层应用程序和另一个顶层类,它们代表一种面板,其中较小的主窗口画布图像用于导航。
我知道我必须保留对我的画布图像的引用,以使其免受垃圾收集的影响,但是,我无法达到预期的结果。
面板类的代码如下:
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:对不起,很长的帖子!
我很抱歉,但是错误出在我用于画布图像的错误锚点选项中。 用anchor ='nw'替换它可以解决所有问题。 看到与未引用图像非常相似的问题的行为非常令人困惑,所以我有点迷路!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.