【发布时间】:2020-08-29 02:47:44
【问题描述】:
所以我想创建一个可以在 python 中点击的按钮。
我正在使用 python turtle 模块制作一个非常简单的游戏。
这是我的代码x 是点击的 x 位置,y 是点击的 y 位置。
screen = turtle.Screen() #for some context
def click(x, y):
if (x <= 40 and x <= -40) and (y <= 20 and y <= -20):
#code here
screen.onscreenclick(click)
我希望它在我单击特定区域时运行一些代码,但是 这段代码对我不起作用。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
code
(x <= 40 and x <= -40)可以简化为(x <= -40)- 可能你使用了错误的<=并且你需要(x <= 40 and x >= -40)或更易读的(-40 <= x <= 40)(y <= 20 and y <= -20)同样的问题 -
你是什么意思“不起作用”?
-
也许先使用
print(x)和print( x <= 40 and x <= -40 )看看你得到了什么。 -
@acw1668 我的意思是当我点击该区域或任何区域时,它不会注册点击。
-
在
click()的开头添加print(x, y),看看在海龟屏幕的任意区域点击是否显示在控制台中。
标签: python python-3.x tkinter turtle-graphics tkinter-canvas