如何在 python 中禁用鼠标事件?
How can I disable mouse events in python?
我在计算机读取其中一个鼠标事件后禁用鼠标事件时遇到了问题。我正在使用乌龟。我的代码:
import turtle
wn = turtle.Screen()
#Box -------------------------- (Apple)
b1 = turtle.Turtle()
b1.color("red")
b1.ht()
b1.speed(0)
b1.penup()
b1.goto(-520, -200)
b1.begin_fill()
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.penup()
b1.end_fill()
#Box2 --------------------------- (Banana)
b2 = turtle.Turtle()
b2.color("yellow")
b2.ht()
b2.speed(0)
b2.penup()
b2.goto(-120, -200)
b2.begin_fill()
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.penup()
b2.end_fill()
#Box3 --------------------------- (Orange)
b3 = turtle.Turtle()
b3.color("orange")
b3.ht()
b3.speed(0)
b3.penup()
b3.goto(280, -200)
b3.begin_fill()
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.penup()
b3.end_fill()
def pos(x,y):
print(x,y)
if x >= -520 and x <= -280 and y >= -300 and y <= -200:
print("apple")
return
if x >= -120 and x <= 120 and y>= -300 and y <= -200:
print("banana")
return
if x >= 280 and x <= 520 and y>= -300 and y <= -200:
print("orange")
return
while True:
wn.onscreenclick(pos)
wn.update()
我要什么:点击一个框后,会读取鼠标事件并打印相应的水果,但仅限于第一次;再次单击一个框时,它不会打印水果。这意味着再次单击 Box2
、Box3
或 Box1
将不再打印相应的水果,例如单击 Box1
时。但它不起作用,即使使用 return
语句也是如此。我该怎么做?
只需使用一个全局布尔变量
clicked = False
def pos(x,y):
global clicked
if clicked: return
clicked = True
print(x,y)
if x >= -520 and x <= -280 and y >= -300 and y <= -200:
print("apple")
return
if x >= -120 and x <= 120 and y>= -300 and y <= -200:
print("banana")
return
if x >= 280 and x <= 520 and y>= -300 and y <= -200:
print("orange")
return
不需要全局布尔值,您可以在点击彩色方块后简单地关闭按钮事件处理程序:
from turtle import Turtle, Screen
def pos(x, y):
print(x, y)
if -520 <= x <= -280 and -300 <= y <= -200:
screen.onscreenclick(None)
print("apple")
elif -120 <= x <= 120 and -300 <= y <= -200:
screen.onscreenclick(None)
print("banana")
elif 280 <= x <= 520 and -300 <= y <= -200:
screen.onscreenclick(None)
print("orange")
screen = Screen()
# Box -------------------------- (Apple)
b1 = Turtle()
b1.ht()
b1.color("red")
b1.speed('fastest')
b1.penup()
b1.goto(-520, -200)
b1.begin_fill()
for _ in range(2):
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.end_fill()
# Box2 --------------------------- (Banana)
b2 = Turtle()
b2.ht()
b2.color("yellow")
b2.speed('fastest')
b2.penup()
b2.goto(-120, -200)
b2.begin_fill()
for _ in range(2):
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.end_fill()
# Box3 --------------------------- (Orange)
b3 = Turtle()
b3.ht()
b3.color("orange")
b3.speed('fastest')
b3.penup()
b3.goto(280, -200)
b3.begin_fill()
for _ in range(2):
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.end_fill()
screen.onscreenclick(pos)
screen.mainloop()
我解决这个问题的方法不是点击屏幕并计算我们在哪个矩形上方,而是用海龟制作矩形,然后让单独的海龟 onclick()
方法完成工作:
from turtle import Turtle, Screen
from functools import partial
WIDTH, HEIGHT = 240, 100
CURSOR_SIZE = 20
def click(fruit, x, y):
print(x, y)
print(fruit)
apple.onclick(None)
banana.onclick(None)
orange.onclick(None)
screen = Screen()
offset = screen.window_width()/2 - WIDTH/2
# Box -------------------------- (Apple)
apple = Turtle()
apple.color("red")
apple.shape("square")
apple.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
apple.penup()
apple.goto(-offset, -150)
apple.onclick(partial(click, "apple"))
# Box2 --------------------------- (Banana)
banana = apple.clone()
banana.color("yellow")
banana.setx(0)
banana.onclick(partial(click, "banana"))
# Box3 --------------------------- (Orange)
orange = banana.clone()
orange.color("orange")
orange.setx(offset)
orange.onclick(partial(click, "orange"))
screen.mainloop()
我在计算机读取其中一个鼠标事件后禁用鼠标事件时遇到了问题。我正在使用乌龟。我的代码:
import turtle
wn = turtle.Screen()
#Box -------------------------- (Apple)
b1 = turtle.Turtle()
b1.color("red")
b1.ht()
b1.speed(0)
b1.penup()
b1.goto(-520, -200)
b1.begin_fill()
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.penup()
b1.end_fill()
#Box2 --------------------------- (Banana)
b2 = turtle.Turtle()
b2.color("yellow")
b2.ht()
b2.speed(0)
b2.penup()
b2.goto(-120, -200)
b2.begin_fill()
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.penup()
b2.end_fill()
#Box3 --------------------------- (Orange)
b3 = turtle.Turtle()
b3.color("orange")
b3.ht()
b3.speed(0)
b3.penup()
b3.goto(280, -200)
b3.begin_fill()
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.penup()
b3.end_fill()
def pos(x,y):
print(x,y)
if x >= -520 and x <= -280 and y >= -300 and y <= -200:
print("apple")
return
if x >= -120 and x <= 120 and y>= -300 and y <= -200:
print("banana")
return
if x >= 280 and x <= 520 and y>= -300 and y <= -200:
print("orange")
return
while True:
wn.onscreenclick(pos)
wn.update()
我要什么:点击一个框后,会读取鼠标事件并打印相应的水果,但仅限于第一次;再次单击一个框时,它不会打印水果。这意味着再次单击 Box2
、Box3
或 Box1
将不再打印相应的水果,例如单击 Box1
时。但它不起作用,即使使用 return
语句也是如此。我该怎么做?
只需使用一个全局布尔变量
clicked = False
def pos(x,y):
global clicked
if clicked: return
clicked = True
print(x,y)
if x >= -520 and x <= -280 and y >= -300 and y <= -200:
print("apple")
return
if x >= -120 and x <= 120 and y>= -300 and y <= -200:
print("banana")
return
if x >= 280 and x <= 520 and y>= -300 and y <= -200:
print("orange")
return
不需要全局布尔值,您可以在点击彩色方块后简单地关闭按钮事件处理程序:
from turtle import Turtle, Screen
def pos(x, y):
print(x, y)
if -520 <= x <= -280 and -300 <= y <= -200:
screen.onscreenclick(None)
print("apple")
elif -120 <= x <= 120 and -300 <= y <= -200:
screen.onscreenclick(None)
print("banana")
elif 280 <= x <= 520 and -300 <= y <= -200:
screen.onscreenclick(None)
print("orange")
screen = Screen()
# Box -------------------------- (Apple)
b1 = Turtle()
b1.ht()
b1.color("red")
b1.speed('fastest')
b1.penup()
b1.goto(-520, -200)
b1.begin_fill()
for _ in range(2):
b1.forward(240)
b1.right(90)
b1.forward(100)
b1.right(90)
b1.end_fill()
# Box2 --------------------------- (Banana)
b2 = Turtle()
b2.ht()
b2.color("yellow")
b2.speed('fastest')
b2.penup()
b2.goto(-120, -200)
b2.begin_fill()
for _ in range(2):
b2.forward(240)
b2.right(90)
b2.forward(100)
b2.right(90)
b2.end_fill()
# Box3 --------------------------- (Orange)
b3 = Turtle()
b3.ht()
b3.color("orange")
b3.speed('fastest')
b3.penup()
b3.goto(280, -200)
b3.begin_fill()
for _ in range(2):
b3.forward(240)
b3.right(90)
b3.forward(100)
b3.right(90)
b3.end_fill()
screen.onscreenclick(pos)
screen.mainloop()
我解决这个问题的方法不是点击屏幕并计算我们在哪个矩形上方,而是用海龟制作矩形,然后让单独的海龟 onclick()
方法完成工作:
from turtle import Turtle, Screen
from functools import partial
WIDTH, HEIGHT = 240, 100
CURSOR_SIZE = 20
def click(fruit, x, y):
print(x, y)
print(fruit)
apple.onclick(None)
banana.onclick(None)
orange.onclick(None)
screen = Screen()
offset = screen.window_width()/2 - WIDTH/2
# Box -------------------------- (Apple)
apple = Turtle()
apple.color("red")
apple.shape("square")
apple.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
apple.penup()
apple.goto(-offset, -150)
apple.onclick(partial(click, "apple"))
# Box2 --------------------------- (Banana)
banana = apple.clone()
banana.color("yellow")
banana.setx(0)
banana.onclick(partial(click, "banana"))
# Box3 --------------------------- (Orange)
orange = banana.clone()
orange.color("orange")
orange.setx(offset)
orange.onclick(partial(click, "orange"))
screen.mainloop()