簡體   English   中英

調用函數時,Python顏色填充不起作用

[英]Python color fill not working when function is called

可以使用幫助來理解為什么我在調用parrallelogram函數時該程序無法填充任何顏色。 我正在運行Python v3。 這是我的代碼。

    import turtle

    d_length = eval(input("Enter inside length of parallelogram: "))
    s = d_length / 2 * math.cos(math.pi / 4)
    parallelogramColor = turtle.fillcolor(" ")

    def parallelogram(s, parallelogramColor):
        for i in range(1):
            turtle.begin_fill()
            turtle.forward(s)
            turtle.left(45)
            turtle.forward(s)
            turtle.left(135)
            turtle.forward(s)
            turtle.left(45)
            turtle.forward(s)
            turtle.left(90)
            turtle.end_fill()

    parallelogram(s, "brown")

fillcolor()的文檔說

如果turtleshape是多邊形,則使用新設置的fillcolor繪制該多邊形的內部。

itself is changed not that of the shape drawn by it. 因此, 本身的顏色不會改變,而不會改變其繪制的形狀。

使用color()代替。 正如其文檔所述,

返回或設置pencolor和fillcolor

因此,您可以將功能更改為

def parallelogram(s, parallelogramColor):
    turtle.color(parallelogramColor)

    turtle.color(parallelogramColor)
    turtle.begin_fill()
    turtle.forward(s)
    turtle.left(45)
    turtle.forward(s)
    turtle.left(135)
    turtle.forward(s)
    turtle.left(45)
    turtle.forward(s)
    turtle.left(90)
    turtle.end_fill()

您可以刪除該循環,因為只有一次迭代。

暫無
暫無

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

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