'RawTurtle' 对象没有属性 'Turtle'

'RawTurtle' object has no attribute 'Turtle'

我在 python 包含的乌龟演示中看到了排序示例,我想在我的程序中添加类似的动画。我的程序基于 tkinter,我想在 tkinter canvas(使用 RawTurtle)中插入乌龟动画,所以首先我尝试在 canvas 中创建一个黑框,然后我得到了以下错误消息:

AttributeError: 'RawTurtle' object has no attribute 'Turtle'

这是我的代码:

import tkinter
from turtle import *

class MyApp():

    def __init__(self, parent):
        self.p = parent
        self.f = tkinter.Frame(self.p).pack()
        self.c = tkinter.Canvas(self.f, height = '640', width = '1000')
        self.c.pack()
        self.t = RawTurtle(self.c)
        self.main(5)

    def main(self, size):
        self.t.size = size
        self.t.Turtle.__init__(self, shape="square", visible=False)
        self.t.pu()
        self.t.shapesize(5, 1.5, 2)
        self.t.fillcolor('black')
        self.t.st()

if __name__ == '__main__':
    root= tkinter.Tk()
    frame = MyApp(root)
    root.mainloop()

你几乎已经掌握了——你试图通过不存在的 Turtle() 实例方法更改的那两个设置可以在创建 RawTurtle:

时处理
import tkinter
from turtle import RawTurtle

class MyApp():

    def __init__(self, parent):
        self.p = parent
        self.f = tkinter.Frame(self.p).pack()
        self.c = tkinter.Canvas(self.f, height=640, width=1000)
        self.c.pack()
        self.t = RawTurtle(self.c, shape='square', visible=False)
        self.main(5)

    def main(self, size):
        self.t.size = size  # does nothing if stamping with pen up
        self.t.penup()
        self.t.shapesize(5, 1.5, 2)
        self.t.fillcolor('black')  # the default
        self.t.stamp()

if __name__ == '__main__':
    root = tkinter.Tk()
    frame = MyApp(root)
    root.mainloop()