Python:如何从类中删除对象?

时间:2016-06-26 15:53:03

标签: python class tkinter

我是python的新手,对象类有点麻烦。我创建的代码是球体对象从墙壁侧面反弹。点击后我想删除一个球。我已经尝试了几种不同的方法,但它们都导致了错误。下面是我从墙上弹出的球的代码。如何编辑此代码以便在单击后删除球?谢谢!

from Tkinter import *
import random
import time

canvasWidth=480
canvasHeight=320


root= Tk()
canvas=Canvas(root,width=canvasWidth,height=canvasHeight, bg='white')
root.title('RaspPi')
canvas.pack()

class Ball:
    def __init__(self):
            self.ballSize = 30
            self.xposition = random.randint(0 + self.ballSize, canvasWidth - self.ballSize)
            self.yposition = random.randint(0 + self.ballSize, canvasHeight - self.ballSize)
            self.shape=canvas.create_oval(self.xposition,self.yposition,self.xposition+self.ballSize,self.yposition+self.ballSize, fill='black',activefill="grey",width=0)
            self.xspeed = random.randrange(-3,3)
            self.yspeed = random.randrange(-3,3)



    def move(self):
        canvas.move(self.shape, self.xspeed, self.yspeed)
        pos = canvas.coords(self.shape)
        if pos[2] >= canvasWidth or pos[0] <= 0:
            self.xspeed = -self.xspeed
        if pos[3] >= canvasHeight or pos[1] <= 0:
            self.yspeed = -self.yspeed


balls=[]
for i in range(20):
    balls.append(Ball())

while True:
    for ball in balls:
        ball.move()

    root.update()
    time.sleep(0.01)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用类画布的bind-method并删除单击的椭圆。你的for循环应该有一个异常处理,因为删除的对象不能有coordiantes或speed。 del()函数通常用于删除对象。

from Tkinter import *
import random
import time

canvasWidth = 480
canvasHeight = 320

root = Tk()
canvas = Canvas(root, width=canvasWidth, height=canvasHeight, bg='white')
root.title('RaspPi')
canvas.pack()


class Ball:
    def __init__(self):
            self.ballSize = 30
            self.xposition = random.randint(0 + self.ballSize, canvasWidth - self.ballSize)
            self.yposition = random.randint(0 + self.ballSize, canvasHeight - self.ballSize)
            self.shape=canvas.create_oval(self.xposition,self.yposition,self.xposition+self.ballSize,self.yposition+self.ballSize, fill='black',width=0)
            self.xspeed = random.randrange(-3,3)
            self.yspeed = random.randrange(-3,3)

    def move(self):
        canvas.move(self.shape, self.xspeed, self.yspeed)
        pos = canvas.coords(self.shape)
        if pos[2] >= canvasWidth or pos[0] <= 0:
            self.xspeed = -self.xspeed
        if pos[3] >= canvasHeight or pos[1] <= 0:
            self.yspeed = -self.yspeed


balls=[]
for i in range(20):
    balls.append(Ball())


def click(event):
    if canvas.find_withtag(CURRENT):
        canvas.delete(CURRENT)

canvas.bind("<Button-1>", click)

while True:
    for ball in balls:
        try:
            ball.move()
        except:
            del(ball)

    root.update()
    time.sleep(0.01)

root.mainloop()

<强>更新

如果列表move不为零,只需检查方法pos即可。如果它为零则只删除对象本身。如果您只是删除画布,但是如果您关心内存使用情况,那么这是我能想到的最佳选择。

from Tkinter import *
import random
import time

canvasWidth = 480
canvasHeight = 320

root = Tk()
canvas = Canvas(root, width=canvasWidth, height=canvasHeight, bg='white')
root.title('RaspPi')
canvas.pack()


class Ball:
    def __init__(self):
            self.ballSize = 30
            self.xposition = random.randint(0 + self.ballSize, canvasWidth - self.ballSize)
            self.yposition = random.randint(0 + self.ballSize, canvasHeight - self.ballSize)
            self.shape=canvas.create_oval(self.xposition,self.yposition,self.xposition+self.ballSize,self.yposition+self.ballSize, fill='black',width=0)
            self.xspeed = random.randrange(-3,3)
            self.yspeed = random.randrange(-3,3)

    def move(self):
        canvas.move(self.shape, self.xspeed, self.yspeed)
        pos = canvas.coords(self.shape)
        if len(pos) != 0:
            if pos[2] >= canvasWidth or pos[0] <= 0:
                self.xspeed = -self.xspeed
            if pos[3] >= canvasHeight or pos[1] <= 0:
                self.yspeed = -self.yspeed
        else:
            del(self)




balls=[]
for i in range(20):
    balls.append(Ball())


def click(event):
    if canvas.find_withtag(CURRENT):
        canvas.delete(CURRENT)

canvas.bind("<Button-1>", click)

while True:
    for ball in balls:
        ball.move()

    root.update()
    time.sleep(0.01)

root.mainloop()