python - Python缺少参数
问题描述
我的错在哪里
“这段代码只是我的代码的一部分,我只是复制了一部分”
import turtle
wn=turtle.Screen()
wn.bgcolor("black")
wn.title("Pacman")
wn.setup(900,700)
class Pacman(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("yellow")
self.penup()
self.speed(0)
def up(self):
self.goto(self.xcor(),self.ycor()+24)
def down(self):
self.goto(self.xcor(),self.ycor()-24)
def left(self):
self.goto(self.xcor()-24,self.ycor())
def right(self):
self.goto(self.xcor()+24,self.ycor())
wn.listen()
wn.onkey(Pacman.down, "Down")
wn.onkey(Pacman.up, "Up")
wn.onkey(Pacman.right, "Right")
wn.onkey(Pacman.left, "Left")
wn.tracer(0)
while True:
wn.update()
失败
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\asus\AppData\Local\Programs\Python\Python37-
32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\asus\AppData\Local\Programs\Python\Python37-
32\lib\turtle.py", line 686, in eventfun
fun()
TypeError: up() missing 1 required positional argument: 'self'
当我单击右、下、上或左按钮时,方块不移动并且在控制台中写入失败
解决方案
“Pacman”是一个类模板,所以你不能像那样调用它的方法。您应该创建一个 Pacman 对象(像这样):
new_pacman = Pacman()
然后你可以像这样运行你的函数:
new_pacman.up()
或者(如果我没记错的话)你可以试试这个,我认为这也应该有效:
Pacman().up() which
推荐阅读
- c++ - 使用多态类实现多线程
- php - PHP - DOMXpath 不应该返回任何节点
- python - ProcessPoolExecutor 中的 max_workers 参数是否代表 CPU 中的核心数?
- php - 我想在数据库中创建一个表,但我希望表的名称是一个日期。但它不起作用
- python - plt.plot 分类轴过度拥挤
- python - Python - 绘图循环中的图形设置
- flask - 更改 Flask 表单文件上传字段中使用的文本
- git - 缺少拉取请求按钮
- linux - 如何在具有非常大行的文件中查找单词差异
- python - AWS lambda:使用子进程导入外部 python 库使程序太慢