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

问题描述 投票:0回答:1

我在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()
python canvas tkinter turtle-graphics
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.