【发布时间】:2016-04-26 21:41:43
【问题描述】:
尝试遍历包含矩形的列表。然后从列表中删除/删除所有橙色的矩形。我为它写了一段代码,但不断收到不可迭代的错误。
from tkinter import *
import random
root = Tk()
from Stack import Stack
from my_queue import *
class Recta:
def __init__(self, height=60, width=80 ,colours= []):
self.height = height
self.width = width
self.canvas = Canvas(root)
self.canvas.pack()
self.colours = ["red", "orange"]
self.rects = []
self.stack = Stack()
self.queue = Queue()
def randomRects(self):
w = random.randrange(300)
h = random.randrange(200)
self.rects.append(self.canvas.create_rectangle(0, 0, w, h, fill= random.choice(self.colours)))
def remove_all_orange_shapes(self):
for i in self.randomRects():
if i == "orange":
return self.canvas.delete(self.rects.pop())
else:
continue
tes = Recta()
tes= Stack()
tes = Queue()
root.mainloop()
【问题讨论】:
-
randomRects返回None... 所以粗略的for item in None是无效的python -
你说出三个对象
tes? -
@PadraicCunningham ...但继续不使用它们...
-
@JoranBeasley,是的,更值得深思。
-
@rprogramr,我建议你阅读教程anandology.com/python-practice-book
标签: python list class python-3.x