简体   繁体   中英

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'

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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