【问题标题】:'RawTurtle' object has no attribute 'Turtle''RawTurtle' 对象没有属性 'Turtle'
【发布时间】:2018-01-01 18:07:17
【问题描述】:

我在海龟演示中看到了 python 包含的排序示例,我想在我的程序中添加类似的动画。我的程序基于 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()

【问题讨论】:

  • 我认为您可以在没有 Turtle 的情况下创建自己的动画 - 它可以更容易控制。

标签: python canvas tkinter turtle-graphics


【解决方案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()

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 2020-07-12
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-08-03
    相关资源
    最近更新 更多