简体   繁体   English

在python中将类方法传递给onscreenclick

[英]Passing a class method to onscreenclick in python

I tried implementing the same in a class and it throws an error 我试图在一个类中实现相同的功能,但会引发错误

class Lumber:
    def doclick(x,y):
        print(str(x) + "" + str(y))
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        self.drawBark()

    mainScreen.onscreenclick(fun = doclick)

Please suggest a way to call the doclick method. 请提出一种调用doclick方法的方法。 I want everything inside my class 我想要班上所有的东西

Does the following meet your needs? 以下满足您的需求吗?

import turtle

mainScreen = turtle.Screen()

class Lumber:
    def doclick(self, x, y):
        print(str(x), str(y))
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        self.drawBark()

    def drawBark(self):
        pass

    mainScreen.onscreenclick(fun=lambda x, y: lumber_instance.doclick(x, y))

lumber_instance = Lumber()

turtle.done()

Or do you want every Lumber instance to add it's own onscreenclick handler? 还是您要每个Lumber实例添加自己的onscreenclick处理程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM