You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter窗口Canvas中RawTurtle报错:NoneType对象无len()方法

问题描述

在使用CustomTkinter构建的PatientWindow类(Toplevel窗口)中,尝试在Canvas组件内通过turtle库的RawTurtle对象绘图时,无法设置位置、获取位置或移动该对象。执行self.wall.setposition((100, 100))时触发TypeError,报错信息为TypeError: object of type 'NoneType' has no len()。怀疑是层级嵌套问题:RawTurtle嵌套在Screen、Canvas、Frame中,且该Toplevel窗口又嵌套在另一窗口类内。

相关代码

class PatientWindow():
    def __init__(self):

        top = customtkinter.CTkToplevel()
        top.geometry("600x850")
        top.title("Exoskeleton Patient View")
        self.frame = customtkinter.CTkFrame(top)

        im = Image.open("images/please-wait2.png")
        resized = im.resize((600, 600), Image.Resampling.LANCZOS)
        ph = ImageTk.PhotoImage(resized)

        self.label = Label(self.frame, image=ph, width=600, height=600)
        self.label.image = ph  # need to keep the reference of your image to avoid garbage collection
        self.label.grid(row=0, column=0)
        self.frame.grid(row=0, column=0)

        w = 600
        h = 250

        self.frame_bottom = customtkinter.CTkFrame(top)
        self.canvas = customtkinter.CTkCanvas(self.frame_bottom, width=w, height=h, bg='gray')
        self.canvas.grid(row=0, column=0)

        x = w // 2
        y = h // 2
        cursor = self.canvas.create_oval(x, y, x+10, y+10, fill='red')

        # left_wall = self.canvas.create_rectangle(125, 90, 100, 165, fill='pink')

        self.frame_bottom.grid(row=1, column=0)

        self.screen = turtle.TurtleScreen(self.canvas)
        self.screen.bgcolor("cyan")
        self.wall = turtle.RawTurtle(self.canvas, shape='turtle')
        self.wall.shapesize(stretch_len=2, stretch_wid=2)
        self.wall.color("green")
        self.wall.setposition((100, 100))

完整报错栈

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\benja\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\benja\ALL CODING\CapstoneGUI\venv\lib\site-packages\customtkinter\widgets\ctk_button.py", line 377, in clicked
    self.command()
  File "C:\Users\benja\ALL CODING\CapstoneGUI\main.py", line 74, in open
    self.patientWindow = PatientWindow()
  File "C:\Users\benja\ALL CODING\CapstoneGUI\main.py", line 150, in __init__
    self.wall.setposition((100, 100))
  File "C:\Users\benja\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1774, in goto
    self._goto(Vec2D(*x))
  File "C:\Users\benja\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 3159, in _goto
    screen._pointlist(self.currentLineItem),
  File "C:\Users\benja\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 754, in _pointlist
    pl = [(cl[i], -cl[i+1]) for i in range(0, len(cl), 2)]
TypeError: object of type 'NoneType' has no len()
解决方案

问题根源

创建RawTurtle时错误传入了self.canvas,实际上RawTurtle需要绑定到已创建的turtle.TurtleScreen实例(即self.screen)才能正常工作。直接传入Canvas会导致turtle内部无法找到正确的屏幕上下文,调用setposition时因缺失必要的上下文数据触发NoneType错误。

修复代码

修改创建RawTurtle的行,将参数从self.canvas替换为self.screen:

# 原错误代码
# self.wall = turtle.RawTurtle(self.canvas, shape='turtle')
# 修改后正确代码
self.wall = turtle.RawTurtle(self.screen, shape='turtle')

效果验证

修改后,self.wall.setposition((100, 100))可正常执行,RawTurtle对象能正确移动到指定位置,不会再触发类型错误。

内容的提问来源于stack exchange,提问作者Ben G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:25:19