當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python turtle.fillcolor()用法及代碼示例

turtle 模塊以麵向對象和麵向過程的方式提供 turtle 圖形基元。由於它使用Tkinter作為基礎圖形,因此需要安裝有Tk支持的Python版本。

turtle .fillcolor()

此方法用於返回或設置填充顏色。如果turtleshape是多邊形,則使用新設置的fillcolor繪製該多邊形的內部。

用法:turtle.fillcolor(*args)

參數:

  • fillcolor():返回當前的fillcolor作為顏色說明字符串,可能為hex-number格式。
  • fillcolor(colorstring):它是Tk顏色規範字符串,例如“red”或“yellow”。
  • fillcolor((r,g,b)):r,g和b的元組代表RGB顏色,並且r,g和b的每個都在0到colormode的範圍內
  • fillcolor(r,g,b):r,g和b表示RGB顏色,並且r,g和b的範圍均為0到colormode。

下麵是上述方法的實現和一些示例:

範例1:



Python3

# importing package 
import turtle 
  
# set turtle 
turtle.shape("turtle") 
turtle.turtlesize(3,3,1) 
  
# check by default value 
print(turtle.fillcolor()) 
  
# set blue color 
turtle.fillcolor("blue") 
  
# check fillcolor value 
print(turtle.fillcolor())

輸出:

black
blue

範例2:

Python3

# importing package 
import turtle 
  
# method to draw a star 
def star():
    for i in range(5):
        turtle.forward(60) 
        turtle.right(144) 
  
# method to set position 
# and fill color in star 
def draw(x,y,col):
    turtle.up() 
    turtle.setpos(x,y) 
    turtle.down() 
    turtle.fillcolor(col) 
    turtle.begin_fill() 
    star() 
    turtle.end_fill() 
  
# Driver Code 
draw(-100,0,"red") 
draw(-50,0,"yellow") 
draw(0,0,"blue") 
draw(50,0,"green") 
  
# hide the turtle 
turtle.hideturtle()

輸出:




相關用法


注:本文由純淨天空篩選整理自deepanshu_rustagi大神的英文原創作品 turtle.fillcolor() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。