Hi folks,

I was wondering if there was some way to delete all Labels from a Tkinter canvas in one go. I'm not even sure if the labels are being drawn on the canvas since you only use the master(self.root) when creating it but I do the same for buttons and self.canvas.delete(ALL) is working for that. The labels are being created in a seperate class but the buttons are not. The class where self.canvas.delete(ALL) is being called is the same one as where the buttons are being created. Darn, I didn't want to post the code since it was so messy but I'll do it. Here it is. Please help:

from Tkinter import *
from PIL import Image, ImageTk

class Slot(object):

    def __init__(self,root,canvas,appimg,x,y):
        self.root=root
        self.canvas=canvas
        self.l=Label(self.root,image=appimg)
        self.l.place(x=x,y=y)

class View(object):
    def __init__(self,canvas):
        self.canvas=canvas

class win1(View):
    def draw(self,root,pl,tv):
        self.canvas.delete(ALL)
        self.root=root
        self.photolist=pl
        self.tv=tv
        self.s=Slot(self.root,self.canvas,self.photolist[0],10,10)
        self.qbtn1=Button(self.root,text="quit",command=lambda:self.quit(self.tv))
        self.qbtn1.place(x=270,y=100)
    def quit(self,tv):
        self.tv[1].draw(self.root,self.photolist,self.tv)

class win2(View):
    def draw(self,root,pl,tv):
        self.canvas.delete(ALL)
        self.root=root
        self.photolist=pl
        self.tv=tv
        self.s2=Slot(self.root,self.canvas,self.photolist[1],270,10)
        self.qbtn2=Button(self.root,text="quit2",command=lambda:self.quit(self.tv))
        self.qbtn2.place(x=100,y=10)
    def quit(self,tv):
        self.tv[0].draw(self.root,self.photolist,self.tv)

class win3(View):
    def draw(self):
        pass

class App(object):
    def openAllImgs(self):
        self.photolist=[]
        image=Image.open("pic1.bmp")
        photo1=ImageTk.PhotoImage(image)
        self.photolist.append(photo1)
        image=Image.open("pic2.bmp")
        photo2=ImageTk.PhotoImage(image)
        self.photolist.append(photo2)


    def __init__(self, width=640, height=480):

        self.width = width
        self.height = height

        self.root = Tk()
        self.root.title("tkinter_test01")
        self.root.geometry("%sx%s"%(self.width, self.height))

        self.canvas = Canvas(self.root, width=self.width, height=self.height)

        self.theviews=[win1(self.canvas),win2(self.canvas),win3(self.canvas)]
        self.openAllImgs()
        self.theviews[0].draw(self.root,self.photolist,self.theviews)
        self.canvas.pack()
        self.root.mainloop()

app=App()

sorry i dont know if my earlier post was displayed. daniweb was saying something about an activation email for membership but hotmail was moving all their stuff to junk.

But forget or place_forget work on a object specific basis right? No forgetting of everything on canvas? I'd probably have to put all the slots I create in a list and apply 'for all in list' to get rid of em. I might as well use destroy also....unless there is some better way?

How else would you program remove-the-labels-only-and-keep-everything-else, except on an individual basis. The way you have it set up you would have to keep track of the class instance for Slot as well as each label belongs to a separate class instance.

self.s=Slot(self.root,self.canvas,self.photolist[0],10,10)
some_list.append(self.s.l)

Edited 3 Years Ago by woooee

actually I dont want to remove the labels only as im trying to wipe everything with self.canvas.delete(ALL) and rebuild everything each time a another View class is drawn. Can you please tell me why this works for my buttons but not for my labels?

This article has been dead for over six months. Start a new discussion instead.