【发布时间】:2014-10-17 12:31:30
【问题描述】:
我已经在这里问过一个类似的问题,并且收到了非常有用的回复。 但是从那以后我修改了我的代码,现在我猜它更优化了,它应该更灵活,但同样的问题仍然存在。我无法删除该类的实例。
我要做的是创建一个圆圈(单击鼠标左键),然后我希望程序删除该圆圈(单击鼠标右键)。
My code:
from 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(self.fen, width = 300, height = 300, bg = 'ivory')
self.can1.grid(row = 1)
self.butt1.grid(row = 2)
self.fen.bind("<Button-1>", self.create_obj)
self.fen.bind("<Button-3>", self.delete_obj)
self.fen.mainloop()
def create_obj(self, event):
self.d = Oval()
self.can1.create_oval(self.d.x1, self.d.y1, self.d.x2, self.d.y2, fill='red', width = 2)
def delete_obj(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()
所以,再一次,问题是这里我不能删除对象:
def delete_obj(self, event):
self.can1.delete(self.d)
还有一个问题。鉴于我只是一个初学者,就班级组织而言,我不知道我是否选择了正确的方法。它看起来像一个组织良好的代码还是我应该在这个阶段改变任何东西?
【问题讨论】:
-
我明白了。我应该将创建圆圈的方法替换为 OVAL 类吗?然后从 Application 类中引用它?
-
我不明白你遵循什么逻辑。您创建自己的类
Oval的实例,同时创建另一个 tkinter 对象。您从未将self.d放在can1上。你需要Oval做什么?
标签: python class instance code-organization