【问题标题】:Python: how to delete object from class?Python:如何从类中删除对象?
【发布时间】:2016-10-28 15:52:59
【问题描述】:

我是 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()

【问题讨论】:

  • 这似乎是 tkinter 方面的问题,而不是 Python 方面的问题。
  • errr,只需从balls列表中删除对应的实例,删除的对象不会被迭代。
  • 这似乎与“从 [a] 类中删除 [ing] 对象 [s]”没有任何关系。您需要阅读 Tkinter API 以了解如何从游戏中移除对象。
  • @Jean-FrançoisFabre 它们仍将显示在 Tkinter 窗口中。它们也需要从画布中删除。

标签: python class tkinter


【解决方案1】:

使用类画布的绑定方法并删除单击的椭圆。你的 for 循环应该有一个异常处理,因为被删除的对象不能有坐标或速度。 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()

更新

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

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()

【讨论】:

  • 非常感谢!!有什么方法可以不使用 try 和 except 吗?
  • 谢谢!你是最棒的!
猜你喜欢
  • 2022-12-11
  • 2021-05-13
  • 1970-01-01
  • 2011-01-12
  • 2014-06-29
  • 2012-04-03
  • 2015-07-26
  • 1970-01-01
  • 2023-01-16
相关资源
最近更新 更多