简体   繁体   English

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

[英]How to create rotated text in Python's turtle graphics?

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

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.但收到错误消息: TypeError: write() got an unexpected keyword argument 'angle'这意味着海龟模块尚不支持文本旋转功能。

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 ?这就提出了一个问题:有人知道一个补丁/修复,它使 turtle 模块能够利用 Python 3 自版本 3.7.2 以来提供的文本旋转功能?

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

import turtle
txt = '𝄖 𝄗 𝄘 𝄙  𝄚 𝄛'
tt = turtle.Turtle()
tt.write(txt, txt_angle=-45)

doesn't raise a TypeError and draws a 45 degree rotated text.不会引发 TypeError 并绘制 45 度旋转文本。

Below the code of the patch followed by a short turtle script code creating attached image out of the rotated text:在补丁代码下方,后跟一个简短的海龟脚本代码,从旋转的文本中创建附加图像:

乌龟旋转的文字星星

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM