Search code examples
pythonpython-3.xturtle-graphicspython-turtle

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. 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 ?


Solution

  • 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)