首页 > 解决方案 > onscreenclick 在乌龟,python 中不起作用

问题描述

我想使用turtle python在屏幕上按下一个按钮。这并不难,但我尝试使用类来完成它并没有用:

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)

我得到这个错误:

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'

标签: python

解决方案


问题是该deletewindow函数需要一个名为 self 的参数,通常会给出该参数,但您输入deletewindownot Window.deletewindow

你应该做

wn.onscreenclick(Window.deletewindow, 1)

推荐阅读