[英]How do I make it so that my program draws all of my rectangles when it completes, instead of once at a time?
My CS professor gave us an assignment to make a program that uses two lists to collect data on rectangles, and then to draw them into a window. 我的CS教授给我们分配了一个作业,该程序使用两个列表在矩形上收集数据,然后将它们绘制到窗口中。 I was able to make the program work by drawing the rectangles one at a time, but he said that I needed to make them all be drawn after the data was collected. 我能够通过一次绘制一个矩形来使程序正常工作,但是他说,我需要在收集数据之后将它们全部绘制出来。 I tried to do this with the variable "Bolorc", but all I was left with was a single rectangle at the end. 我尝试使用变量“ Bolorc”来执行此操作,但最后只剩下一个矩形。 What am I doing wrong? 我究竟做错了什么?
from graphics import *
def main():
print('Enter data and I will draw you a rectangle.')
win = GraphWin('Rectangle Painter v1.0', 500, 500)
# The "Big Ole' List of Rectangle Coordinates"
Bolorc = []
# Asking for data to draw Rectangles
numRect = int(input('How many rectangles would you like to create? '))
for i in range(numRect):
rec = input('Enter your information for Rectangle #' + str(i + 1) + ' (Ex: red x1 x2 y1 y2): ')
rec1 = rec.split(sep = ' ')
Bolorc = rec1 + Bolorc
# Drawing Rectangles
for i in range(numRect):
Rec = Rectangle(Point(Bolorc[1],Bolorc[2]), Point(Bolorc[3], Bolorc[4]))
Rec.setFill(Bolorc[0])
Rec.setOutline('black')
Rec.setWidth('2')
Rec.draw(win)
main()
You are looping all the rectangles and are drawing only the first one. 您正在循环所有矩形,并且仅绘制第一个矩形。
Rec = Rectangle(Point(Bolorc[1],Bolorc[2]), Point(Bolorc[3], Bolorc[4]))
Rec.setFill(Bolorc[0])
draws only the first rectangle. 仅绘制第一个矩形。
You have to change it so that the rectangle drawn differs according to i
. 您必须对其进行更改,以使绘制的矩形根据i
不同而不同。
Inside your for
loop, 在您的for
循环中,
offset = i * 5 # each rectangle is made up with 5 values
Rec = Rectangle(
Point(Bolorc[offset + 1], Bolorc[offset + 2]),
Point(Bolorc[offset + 3], Bolorc[offset + 4]))
Rec.setFill(Bolorc[offset])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.