簡體   English   中英

循環來自graphics.py變量的顏色

[英]Loop colours from variables for graphics.py

我正在創建一個圖形程序,該程序根據用戶指定的網格大小彼此相鄰繪制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)

我認為我應該以某種方式使用列表,但無法確切地知道如何去做。

如果要給定用戶變量選擇4種顏色,則可以使用模數。

color_index = user_var % 4

那么您可以將顏色變為:

mycolor = colours[color_index]

另外,如果您想在每次用戶需要時提供不同的顏色,並在可用顏色用盡時對其進行循環,則可以使用itertools.cycle iterator:

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'

通過這種方式,您可以獲得:

在此處輸入圖片說明

和代碼:

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()

我不知道您的問題是什么,但是您校准了從未真正使用過的變量col1-col4。 同樣,在范圍(len(iterable))上循環也是很愚蠢的。 這是一個簡化的版本。

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)

“是否可以根據用戶提供的變量來循環顯示顏色?”

是。 您已經在這樣做了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM