简体   繁体   English

如何使程序完成时绘制所有矩形,而不是一次绘制一次?

[英]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.

相关问题 如何使我的代码循环遍历页面上的所有项目? - How do I make it so my code loops through all items on a page? 如何让我的机器人狙击每台服务器获取消息而不是获取所有服务器? - How do I make my bots snipe get messages per server instead of getting all the servers? 按下鼠标按钮后,如何使程序不会无限循环运行 - How can I make my program not run in an endless loop once my mouse button is pressed Selenium Python - 如何让我的程序在某个特定元素出现时单击它? - Selenium Python - How do I make my program to click an particular element when it appeared? 如何获取程序以在嵌套循环中更新此tkinter标签,从而显示灰度过程? - How do I get my program to update this tkinter label in my nested loop so that it shows the grayscale process? 我如何使我的python程序写入新文件 - How do I make my python program to write a new file 如何使我的for循环仅打印一次,而不是为每个i值打印一次 - how to make my for loop print only once instead of for each value of i 一旦所有页面都循环并将值附加到我的列表中,如何将我的刮取值存储到数据框中? - How do I store my scraped values into a dataframe once all the pages are looped and values append into my lists? 刽子手问题:当输入的字母不存在时,如何重置循环?如何让字母在单词中出现多次? - Hangman Issue: How do I make my loop reset when the letter entered is not present and how do I make the letter appear more than once in the word? 我正在尝试制作骰子游戏,以便代替改变每个骰子的骰子数来改变所有骰子 - I am trying to make my dice game so that instead of it changing one of numbers of the dice each roll it changes all of them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM