Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

onscreenclick not working in turtle, python

I want to make a button be pressed on a screen using turtle python. Its not difficult, but I tried to do it using a class and it didn't work:

class Window:
    def __init__(self):
         self.name = 'test'

    def deletewindow(self, x, y):
         if x < 10 and x > 0 and y < 10 and y > 0:
             del self

wn.listen()
wn.onscreenclick(deletewindow, 1)

And I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
      return self.func(*args)
   File "/usr/lib/python3.8/turtle.py", line 675, in eventfun
      fun(x, y)
TypeError: deletewindow() missing 1 required positional argument: 'y'
question from:https://stackoverflow.com/questions/65911458/onscreenclick-not-working-in-turtle-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is that the deletewindow function requires a parameter called self, which usually would be given but you put deletewindow not Window.deletewindow.

You should do

wn.onscreenclick(Window.deletewindow, 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...