python - Delete an instance of a class -
I have already asked a similar question and I have received quite a useful answer. But since then I have modified my code, now it is more favorable, I think, it should be more flexible, but the same problem persists. I can not remove an event in class
What am I trying to do to create a circle (on the left click) and then I hope the program will have to destroy the circle (right-click on the right).
My code: to tkinter import * class Application: def __init __ (self): self.fen = Tk () self.fen.title ('Rom-rom-roooooom') Self. Butt1 = button (self.fen, text = 'quit', command = self.fen.quit) self.can1 = canvas (selfie, width = 300, height = 300, bg = 'ivory') itself .can1.grid ( Line = 1) self.butt1.grid (line = 2) self.fen.bind ("& lt; button-1;", self.create_obj) self.fen.bind ("& lt; button-3" ", Self.delete_obj) self.fen.mainloop () def create_obj (self, incident): self.d = oval () self.can1.create_oval (self.d.x1, self.d.y1, self.d ( Self, event: self.can1.delete (self.d) class oval: def __init __ (self): self.x1 = 50 Self.y1 = 50 self.x2 = 70 self.y2 = 70 appp = application ()
So, once more, The funny thing is that I can not remove the object:
def delete_obj (auto, event): self.can1.delete (self.d)
One more question, seeing the fact that I am just an eater, I do not know if I have chosen the right approach in relation to the organization of the class. Does it look like a well-organized code or can I change anything at this level already?
These two lines:
self.d = oval () Self.can1.create_oval (self.d.x1, self.d.y1, self.d.x2, self.d.2, filling = 'red', width = 2)
Create a new oval
object, name that object as self.d
, then create an oval on self.can1
assign < Absolutely unrelated (excluding one-dimensional characteristics)> self.d
by code> oval object. Instead, I think you want:
o = oval () self.d = self.can1.create_oval (o.x1, o.y1, o.x2, o Y2, fill = 'red', width = 2)
It retains the reference of the object on the canvas
, so that you delete oval
is more or less completely meaningless, because whatever it does, it provides the dimension.
Comments
Post a Comment