Python Forum

Full Version: Delete an object created from another class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,I'm still confuse how multiple class interact between eachother, undestanding why in the following code, you can create a line but not delete it would help.
When I click butB, nothing happens, could anyone tell me why ? Thanks

from tkinter import *

class FaceDom(object):
	def __init__(self, can):
		self.can =can
		self.redline=self.can.create_line(10, 10, 90, 90, fill ='red',width=5)
	
class Projet(Frame):
	def __init__(self, larg, haut):
		Frame.__init__(self)
		self.larg, self.haut = larg, haut
		self.can = Canvas(self, bg='dark green', width =larg, height =haut)
		self.can.pack()
		bList = [("ligne", self.butA),("Delete",self.butB)]
		for b in bList:
			Button(self, text =b[0], command =b[1]).pack()
		self.pack()

	def butA(self):		
		self.x=FaceDom(self.can)

	def butB(self):
		self.can.delete(self.x)
		
Projet(100, 100).mainloop()
when the line was created its id was stored as an attribute redline of an instance of FaceDom
change
self.can.delete(self.x)
to
self.can.delete(self.x.redline)
Thanks, I don't understand yet. So if I don't add "self.readlin=..", how can it be deleted ?

from tkinter import *

class FaceDom(object):
	def __init__(self, can):
		self.can =can
		self.can.create_line(10, 10, 90, 90, fill ='red',width=5)
	
class Projet(Frame):
	def __init__(self, larg, haut):
		Frame.__init__(self)
		self.larg, self.haut = larg, haut
		self.can = Canvas(self, bg='dark green', width =larg, height =haut)
		self.can.pack()
		bList = [("ligne", self.butA),("Delete",self.butB)]
		for b in bList:
			Button(self, text =b[0], command =b[1]).pack()
		self.pack()

	def butA(self):		
		self.x=FaceDom(self.can)
		print(self.x)

	def butB(self):
		self.can.delete(self.x)
		
Projet(100, 100).mainloop()
IS this homework? You have asked a few questions with similar tasks but slightly different code.
Yes,homework, following exercices in a book to learn Python

Ok, so I'll use id then
thanks