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

VS Code中运行Python turtle代码触发AttributeError错误如何解决

错误含义
  • 报错AttributeError: 'function' object has no attribute 'title'指的是你正在操作的wn变量是函数类型,函数本身不存在title属性,无法调用该方法。
  • 问题根源出在赋值语句wn=turtle.Screen:turtle.Screen是屏幕类的构造函数,你没有加括号执行实例化操作,所以wn存储的是函数本身,而非你需要的屏幕实例对象。
修复方法

需要修改两处代码错误:

  1. 实例化屏幕对象:将wn = turtle.Screen改为wn = turtle.Screen(),添加括号完成实例化,此时wn才是可以调用属性方法的屏幕对象
  2. 修正参数拼写错误:原代码中的heigth为拼写错误,正确参数名为height

修复后可运行的完整代码

#Setup
import turtle

wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

#Main game loop
while True:
    wn.update()

内容的提问来源于stack exchange,提问作者Léo Lepage

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 15:54:04