我不知道如何使用
create_text
方法向文本添加笔划。
方法本身没有定义
outline
选项,有人知道方法吗?
谢谢!
据我所知,没有内置方法可以向文本添加笔划,但您可以配置自己的方法。这种方法只需制作粗体文本并在其上覆盖常规文本即可实现:
def stroke_text(x, y, text, textcolor, strokecolor):
# make stroke text
canvas.create_text(x, y, text=text, font=('courier', 16, 'bold'), fill=strokecolor)
# make regular text
canvas.create_text(x, y, text=text, font=('courier', 16), fill=textcolor)
root = Tk()
canvas = Canvas(root, bg='black')
canvas.pack()
stroke_text(100, 50, 'hello', 'white', 'red')
mainloop()
虽然这可能看起来更像是阴影而不是描边;可能有一种方法可以改善这一点。
我参加这个聚会太晚了,但效果不错的是: 我们在空间中放置了几个带有偏移量的文本,结果看起来令人信服
def create_stroked_text(
canvas,
x,
y,
text,
stroke_color,
fill_color,
stroke_width=1,
anchor="ne",
font_size=12,
):
text_font = font.Font(family="Helvetica", size=font_size, weight="bold")
# iterates in space in all directions and draws text
# TODO better to iterate in a circle by angle here
for dx in (-stroke_width, 0, stroke_width):
for dy in (-stroke_width, 0, stroke_width):
if dx != 0 or dy != 0:
canvas.create_text(
x + dx,
y + dy,
text=text,
font=text_font,
fill=stroke_color,
anchor=anchor,
)
# place actual text here
canvas.create_text(
x, y, text=text, font=text_font, fill=fill_color, anchor=anchor
)