【问题标题】:How to create rotated text in Python's turtle graphics?如何在 Python 的海龟图形中创建旋转文本?
【发布时间】:2022-06-25 06:14:57
【问题描述】:

我正在使用带有 tkinter 和 tcl 8.6 版的 Python 3.9,因此根据 2010 年 stackoverflow 上的answer ,我应该能够绘制旋转文本。实际上我可以在 tkinter 中绘制旋转文本,所以我尝试使用 turtle 模块创建一个旋转文本:

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

但收到错误消息:TypeError: write() got an unexpected keyword argument 'angle',这意味着海龟模块尚不支持文本旋转功能。

这引发了一个问题:有人知道一个补丁/修复程序,它使海龟模块能够利用 Python 3 自版本 3.7.2 以来提供的文本旋转功能?

【问题讨论】:

    标签: python python-3.x turtle-graphics python-turtle


    【解决方案1】:

    我已经编写了一个补丁代码,如果将其作为标题添加到海龟 Python 脚本中,则允许使用关键字参数 txt_angle 来创建旋转文本,因此代码:

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

    不会引发 TypeError 并绘制 45 度旋转文本。

    补丁代码下面是一个简短的海龟脚本代码,从旋转的文本中创建以下图像:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多