【问题标题】:Where is my Tkinter Canvas image?我的 Tkinter 画布图像在哪里?
【发布时间】:2014-12-27 23:17:49
【问题描述】:

我在单独的类中显示画布图像时遇到了一些问题。

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

我知道我必须保留对我的画布图像的引用,以使其免受垃圾回收的影响,但是,我未能达到预期的结果。

面板类的代码如下:

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:很抱歉发了这么长的帖子!

【问题讨论】:

    标签: python canvas tkinter


    【解决方案1】:

    非常抱歉,错误在于我用于画布图像的错误锚选项。用 anchor='nw' 替换它解决了一切。看到与不引用图像的问题如此相似的行为非常令人困惑,所以我有点迷路了!

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2021-05-31
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      相关资源
      最近更新 更多