2017-10-17 116 views
0

每次運行代碼時,我都需要爲不同大小的圓圈填充不同的顏色。如何將隨機顏色添加到隨機圓形

from graphics import* 
from random import* 
from time import* 

circle_x=0 
circle_y=0 
colors =0 

#Graphics Window 
def main(): 
    win = GraphWin("Bubbles", 500,500) 
    message = Text(Point(250,200),"Click anywhere to continue") 
    message.draw(win) 
    win.getMouse() 
    message.undraw() 
main()  

#Create Circle 
def create(): 
    win = GraphWin("Bubbles", 500,500) 
    for i in range (4): 

    # Creating a random point for the x of the circle 
     circle_x = randint(50,450) 

    #Creating a random point for the y of the circle 
     circle_y = randint(0,100) 

     p = Point(circle_x,circle_y) 

     radius_x = randint(3,20) 
     c = Circle(p,radius_x) 

     colors = ("salmon","red","blue","green","purple","orange","yellow") 
     fill = choice (colors) 

     c.draw(win) 

我已經遠遠做到了這一點,但不知何故,顏色沒有得到填充。 我需要使用choice

+2

對不起,但是:爲什麼當你可以稱它爲'create_circle'時,你在上面評論過你的函數? – byxor

+0

在您的代碼中包含您的導入。你是從'random'模塊,'turtle'模塊導入的嗎? 你做過獨家進口嗎? – 0TTT0

+0

@ 0TTT0我編輯過它。我不喜歡用烏龜。 –

回答

1
colors = ("salmon","red","navy","steelblue","wheat","darkorange","yellow") 
     fill = choice (colors) 
     c.setFill(fill) 
     c.draw(win)  

解決了問題!