“RawTurtle”对象没有“Turtle”属性

2024-10-01 15:29:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我的代码是:

^{pr2}$

Tags: 程序消息示例object排序demotkinter画布
1条回答
网友
1楼 · 发布于 2024-10-01 15:29:42

在创建RawTurtle时,可以处理通过不存在的Turtle()实例方法更改的两个设置:

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

相关问题 更多 >

    热门问题