Python Forum
[Tkinter] Delete an object created from another class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Delete an object created from another class
#1
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()
Reply
#2
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)
Reply
#3
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()
Reply
#4
IS this homework? You have asked a few questions with similar tasks but slightly different code.
Recommended Tutorials:
Reply
#5
Yes,homework, following exercices in a book to learn Python

Ok, so I'll use id then
thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter moving an class object with keybinds gr3yali3n 5 3,183 Feb-10-2021, 09:13 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020