簡體   English   中英

onscreenclick 在海龜中不起作用,python

[英]onscreenclick not working in turtle, python

我想使用烏龜 python 在屏幕上按下按鈕。 這並不難,但我嘗試使用 class 來做到這一點,但沒有成功:

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'

問題是deletewindow function 需要一個名為 self 的參數,通常會給出該參數,但您將deletewindow放入而不是Window.deletewindow

你應該做

wn.onscreenclick(Window.deletewindow, 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM