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

时间:2018-01-01 18:07:17

标签: python canvas tkinter turtle-graphics

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

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

1 个答案:

答案 0 :(得分:1)

您几乎拥有它 - 在创建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()