从graphics.py的变量循环颜色

时间:2011-11-20 16:41:50

标签: python python-3.x

我正在创建一个图形程序,根据用户指定的网格大小,彼此相邻地绘制100 x 100个正方形。用户还为要着色的正方形输入4种颜色(例如,如果它们输入红色,绿色,蓝色,黄色,则正方形将按该顺序着色,重复颜色)。

是否可以循环用户提供的变量中的颜色?

这是我到目前为止所做的:

def main():
    print ("Please enter four comma seperated colours e.g.: 'red,green,blue,yellow'\n\
    Allowed colours are: red, green, blue, yellow and cyan")
    col1, col2, col3, col4 = input("Enter your four colours: ").split(',')
    win = GraphWin ("Squares", 500, 500)
    colours = [col1, col2, col3, col4]
    drawSquare (win, col1, col2, col3, col4, colours)
    win.getMouse()
    win.close()


def drawSquare(win, col1, col2, col3, col4, colours):
    for i in range (4):
        for j in range (len(colours)):
            colour = colours[j]
            x = 50 + (i * 50)
            circle = Circle (Point (x,50), 20)
            circle.setFill(colour)
            circle.draw(win)

我认为我应该以某种方式使用列表,但无法确切地知道如何操作。

2 个答案:

答案 0 :(得分:0)

如果要在给定用户变量的情况下选择4种颜色,可以使用模数。

color_index = user_var % 4

然后你可以得到你的颜色:

mycolor = colours[color_index]

或者,如果你想在每次用户需要时给出不同的颜色并在可用颜色耗尽时循环它们,你可以使用itertools.cycle迭代器:

In [21]: colours = ['r','b','y','c']
In [22]: from itertools import cycle
In [23]: color = cycle(colours)

In [25]: color.next()
Out[25]: 'r'

In [27]: color.next()
Out[27]: 'b'

In [28]: color.next()
Out[28]: 'y'

In [29]: color.next()
Out[29]: 'c'

In [30]: color.next()
Out[30]: 'r'

In [31]: color.next()
Out[31]: 'b'

通过这种方式你可以得到:

enter image description here

和代码:

from graphics import GraphWin, Rectangle, Point
from itertools import cycle

def main():
    print (("Please enter four comma seperated colours e.g.:"
             "'red,green,blue'\n" 
             "Allowed colours are: red, green, blue, yellow and cyan"))
    colours = input("Enter your four colours: ").split(',')
    print ("Please enter gridsize e.g.: '100'")
    gsize = int(input("Enter gridsize: "))
    win_size = 250
    win = GraphWin("Squares", win_size, win_size)
    drawSquares(win, gsize, win_size, colours)
    win.getMouse()
    win.close()

def drawSquares(win, gsize, winsize, colours):
    side = winsize / gsize
    color = cycle(colours)
    for row in range(gsize):
        y1 = row * side
        y2 = y1 + side
        for column in range(gsize):
            x1 = column * side
            x2 = x1 + side
            rect = Rectangle(Point(x1, y1), Point(x2, y2))
            rect.setFill(color.next())
            rect.draw(win)

main()

答案 1 :(得分:0)

我不知道你的问题是什么,但是你调用了你从未真正使用的变量col1-col4。此外,循环范围(len(可迭代))是愚蠢的。这是一个简化版本。

def main():
    print ("Please enter four comma seperated colours e.g.: 'red,green,blue,yellow'\n\
    Allowed colours are: red, green, blue, yellow and cyan")
    colours = input("Enter your four colours: ").split(',')
    win = GraphWin ("Squares", 500, 500)
    drawSquare (win, colours)
    win.getMouse()
    win.close()


def drawSquare(win, colours):
    for i in range (4):
        for colour in colours:
            x = 50 + (i * 50)
            circle = Circle (Point (x,50), 20)
            circle.setFill(colour)
            circle.draw(win)

“是否可以循环用户提供的变量中的颜色?”

是。你已经这样做了。