如何在Python的Turtle图形中创建旋转的文本?

发布于 2025-02-10 09:29:24 字数 217 浏览 0 评论 0原文

我正在使用TKINTER和TCL版本8.6使用Python 3.9,因此根据答案应该能够绘制旋转的文本。实际上,我能够在tkinter中绘制旋转的文本,因此我也尝试使用Turtle模块写作旋转的文本:

import turtle
txt = '
              

I am using Python 3.9 with tkinter and tcl version 8.6, so according to an answer here on stackoverflow from the year 2010, I should be able to draw rotated texts. And actually I am able to draw rotated texts in tkinter so I tried to create a rotated text with the turtle module too writing:

import turtle
txt = '???? ???? ???? ????  ???? ????'
tt = turtle.Turtle()
tt.write(txt, angle=-45)

but got an error message: TypeError: write() got an unexpected keyword argument 'angle' which means that the turtle module does not yet support the text rotating feature.

This rises the question: Do someone know about a patch/fix which enables the turtle module to make use of the text rotation feature available in Python 3 since version 3.7.2 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-17 09:29:24

我已经编写了一个补丁的代码,如果添加为乌龟python脚本的标题,则允许使用关键字参数txt_angle用于创建旋转的文本,以便代码:

import turtle
txt = '

I have written code of a patch which if added as a header to the turtle Python script allows to use the keyword argument txt_angle for creating rotated texts, so that the code:

import turtle
txt = '???? ???? ???? ????  ???? ????'
tt = turtle.Turtle()
tt.write(txt, txt_angle=-45)

doesn't raise a TypeError and draws a 45 degree rotated text.

Below the code of the patch followed by a short turtle script code creating attached image out of the rotated text:

turtle rotated text star

class Patch_txt_angle:
    def RawTurtleDOTwrite(self, arg, move=False, align="left", font=("Arial", 11, "normal"), txt_angle=0):
        if self.undobuffer:
            self.undobuffer.push(["seq"])
            self.undobuffer.cumulate = True
        end = self._write(str(arg), align.lower(), font, txt_angle)
        if move: x, y = self.pos() ; self.setpos(end, y)
        if self.undobuffer: self.undobuffer.cumulate = False
    def RawTurtleDOT_write(self, txt, align, font, txt_angle):
        item, end = self.screen._write(self._position, txt, align, font, self._pencolor, txt_angle)
        self.items.append(item)
        if self.undobuffer: self.undobuffer.push(("wri", item))
        return end
    def TurtleScreenBaseDOT_write(self, pos, txt, align, font, pencolor, txt_angle):
        x, y = pos ; x = x * self.xscale ; y = y * self.yscale
        anchor = {"left":"sw", "center":"s", "right":"se" }
        item = self.cv.create_text(x-1, -y, text = txt, anchor = anchor[align],
            fill = pencolor, font = font, angle = txt_angle)
        x0, y0, x1, y1 = self.cv.bbox(item)
        self.cv.update()
        return item, x1-1
import turtle
turtle.RawTurtle.write         = Patch_txt_angle.RawTurtleDOTwrite
turtle.RawTurtle._write        = Patch_txt_angle.RawTurtleDOT_write
turtle.TurtleScreenBase._write = Patch_txt_angle.TurtleScreenBaseDOT_write
# ======================================================================
txt = '???? ???? ???? ????  ???? ????'
tt = turtle.Turtle()
sc = turtle.Screen() ; sc.bgcolor("black")
for enum_index, txt_angle in enumerate(range(0, 36000, 1125)):
    if (enum_index)%4 == 0: tt.color("green" ); fontsize=10
    if (enum_index)%4 == 1: tt.color("yellow"); fontsize=15
    if (enum_index)%4 == 2: tt.color("red"   ); fontsize=10
    if (enum_index)%4 == 3: tt.color("orange"); fontsize=13
    txt_angle /= 100    
    tt.setheading(txt_angle); tt.forward(100)
    tt.write(txt, font=("Arial", fontsize, "bold"), align="right", txt_angle=txt_angle)
    tt.backward(100)
from time import sleep; sleep(30)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文