只是想知道,是否可以用半透明墨水绘制/填充乌龟?
类似:
turtle.setfillopacity(50) # Would set it to 50% transparency
运行 python 2.7
你可以这样做
import turtle
turtle = turtle.Turtle()
r = 100
g = 100
b = 100
a = 0.5
turtle.color(r,g,b,a)
(好吧,也许它只适用于 repl.it)
这是不可能做到的,但您可以定义颜色,然后定义光等效值,然后使用它们。
Red = (255,0,0,0)
LRed = (100,0,0)
我认为这会达到类似的效果。当您想要半透明时,您可以使用较浅的颜色。
好吧,你可以使用RGBA。
首先,放入正常的语句:
import turtle
t = turtle.Turtle()
然后,使用
t.color()
,但使用 RGBA。t.color(0,0,0,.5)
50% 的不透明度会让你变黑。
也许这会帮助你达到你想要的结果。
import turtle
# Create a turtle object
tr = turtle.Turtle()
# Set up the screen
screen = turtle.Screen()
screen.setup(800, 600)
# Set the background color with alpha value (RGBA format)
screen.bgcolor((1, 1, 1, 0.5)) # 50% transparency (0.5 alpha value)
# Draw using the turtle
tr.begin_fill()
tr.circle(80)
tr.end_fill()
# Keep the turtle window open until it's closed by the user
turtle.done()
这里,使用元组 (1, 1, 1, 0.5) 调用 screen.bgcolor() 方法来设置背景颜色。该元组表示 RGBA 值,其中 (1, 1, 1) 对应于全白,0.5 对应于 50% 不透明度。
这个 python 海龟示例淡出文本,同时保持原始海龟标记不变:
import turtle
import time
alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.pencolor((0, 0, 0)) #black
alex_text.write("hello")
time.sleep(1)
alex_text.clear()
alex_text.pencolor((.1, .1, .1)) #dark grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.5, .5, .5)) #Grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.8, .8, .8)) #Light grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((1, 1, 1)) #white
alex_text.write("hello")
time.sleep(1)
alex_text.clear() #gone
time.sleep(1)
文本模拟不透明度增加到最大值。 Alex 的邮票未经修改。
无法直接应用 RGBA 颜色,因为
turtle.pencolor()
或 turtle.fillcolor()
根本不支持它。或者,我尝试通过 turtle
创建纯色部分,并且该部分需要使用 matplotlib
不透明,这要感谢 @Don-kirkby 开发的 svg-turtle 包。以下是如何实现这一点的示例:
pip install svg_turtle
pip install matplotlib
pip install svgutils
import turtle
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import svgutils.transform as st
from svg_turtle import SvgTurtle
import os
def draw_turtle_part(file_path):
# draw a circle with solid color
t = SvgTurtle(100, 100)
t.up()
t.goto(0, -50)
t.down()
t.begin_fill()
t.fillcolor('red')
t.circle(40)
t.end_fill()
# save it as a svg file
t.save_as(file_path)
return
def draw_opacity_part(file_path):
# draw a triangle with a color of 50% opacity
fig, ax = plt.subplots(figsize=(10, 10), dpi=300)
ax.set_xlim(-50, 50) # set the canvas size scale the same as turtle
ax.set_ylim(-50, 50) # set the canvas size scale the same as turtle
ax.axis('off') # hide the axis
# draw and fill a triangle with an RGBA color
polygon = patches.Polygon(
[(-50, 0), (0, 50), (50, 0), (-50, 0)],
closed=True, linewidth=0, fill=True, color=(0, 0.8, 0, 0.5))
ax.add_patch(polygon)
# save the figure, remove the paddings and white space surrounding the plot
plt.savefig(file_path, format='svg', transparent=True, bbox_inches='tight', pad_inches=0)
return
def combine_two_parts(file_1, file_2):
x, y = 100, 100
fig1 = st.fromfile(file_1)
# resize the figure to make sure the two align
fig1.set_size((f'{x}pt', f'{y}pt'))
fig2 = st.fromfile(file_2)
# resize the figure to make sure the two align
fig2.set_size((f'{x}px', f'{y}px'))
fig1.append(fig2)
fig1.save('result.svg')
if __name__ == '__main__':
draw_turtle_part('test1.svg')
draw_opacity_part('test2.svg')
combine_two_parts('test1.svg', 'test2.svg')
os.remove('test1.svg') # optional
os.remove('test2.svg') # optional
这会给你这样的结果: 。老实说,现在到了 2023 年,如果绘画本身不太复杂,你还可以使用其他库(如 matplotlib、seaborn、plotly 或 PIL)从头开始绘制你的绘画,尽管我仍然发现
turtle
有时对于绘制曲线或圆很有用。
有一个 YouTube 视频,讲述有人在 here 使用 rgba touple 定义的颜色来实现透明形状 评论中的一些人还建议只使用 rgba 值作为颜色,但我只是以任何可能的方式尝试实现它时遇到错误
另一方面,论坛上有关于 tk 不支持 PNG 图像之外的 Alpha 通道的讨论,这显然意味着海龟即使想要也无法做到这一点
我开始怀疑这是否是版本问题,但我有当前的 python 版本,但无法找到海龟的任何专用更新
如果您想要完全不透明,可以使用
turtle.hideturtle()
来完成此操作。
就像最后一行中使用的那样:
import turtle
t = turtle.Turtle()
t.speed(1)
t.color("blue")
t.begin_fill()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("red")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("green")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("yellow")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.hideturtle()