user3876764
user3876764

Reputation: 21

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. I want everything inside my class

Upvotes: 0

Views: 285

Answers (1)

cdlane
cdlane

Reputation: 41905

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?

Upvotes: 1

Related Questions