Python:如何从类中删除对象?
我是python新手,在对象类方面有点麻烦。我已经创建了一些代码,让球对象从墙上反弹。我想在点击一个球后删除它。我尝试过几种不同的方法,但都会导致错误。下面是我对从墙上弹起的球的代码。如何编辑此代码以在单击球后将其删除?谢谢Python:如何从类中删除对象?,python,class,tkinter,Python,Class,Tkinter,我是python新手,在对象类方面有点麻烦。我已经创建了一些代码,让球对象从墙上反弹。我想在点击一个球后删除它。我尝试过几种不同的方法,但都会导致错误。下面是我对从墙上弹起的球的代码。如何编辑此代码以在单击球后将其删除?谢谢 from Tkinter import * import random import time canvasWidth=480 canvasHeight=320 root= Tk() canvas=Canvas(root,width=canvasWidth,heigh
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导入*
随机输入
导入时间
画布宽度=480
画布高度=320
root=Tk()
画布=画布(根,宽度=画布宽度,高度=画布高度,bg='white')
root.title('RaspPi')
canvas.pack()
班级舞会:
定义初始化(自):
自球尺寸=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移动(自我):
canvas.move(self.shape、self.xspeed、self.yspeed)
pos=画布坐标(自形)
如果pos[2]>=canvasWidth或pos[0]=canvasHeight或pos[1]使用类canvas的bind方法并删除单击的椭圆。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()
这似乎是tkinter端的问题,而不是Python端的问题。errr,只要从balls
列表中删除相应的实例,删除的对象就不会被迭代。这似乎与“从[a]类中删除对象”无关。您需要阅读Tkinter API才能了解如何从游戏中删除对象。@Jean-Françoisfare Tkinter窗口中仍将显示这些对象。它们也需要从画布上删除。非常感谢!!有没有不使用“尝试和例外”的方法来执行此操作?谢谢!你是最棒的!
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()