为了账号安全,请及时绑定邮箱和手机立即绑定

Python 缺少参数

Python 缺少参数

千巷猫影 2021-09-02 20:11:32
我的错在哪里“这段代码只是我代码的一部分,我只是复制了其中的一部分”import turtlewn=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 callbackTraceback (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 eventfunfun()TypeError: up() missing 1 required positional argument: 'self'当我单击右、下、上或左按钮方块不动并且在控制台中写入此失败时
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

“Pacman”是一个类模板,所以你不能像那样调用它的方法。您应该创建一个 Pacman 对象(像这样):

new_pacman = Pacman()

然后你可以像这样运行你的函数:

new_pacman.up()

或者(如果我没记错的话)你可以试试这个,我认为这也应该有效:

Pacman().up() which


查看完整回答
反对 回复 2021-09-02
?
qq_笑_17

TA贡献1818条经验 获得超7个赞

pacman_instance = Pacman()正确的事wn.listen()

问题是您正在尝试对类而不是类的实例进行操作


查看完整回答
反对 回复 2021-09-02
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

如果你只是Pacman = Pacman()在 line 之前添加wn.listen(),它会起作用!

问题是您正在尝试访问未实例化的类,并且使用此行创建此实例,一切都会完美运行;)


查看完整回答
反对 回复 2021-09-02
  • 3 回答
  • 0 关注
  • 207 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号