您在第一win.checkMouse()
忘记()
在你的榜样,你有,因为第一次点击(和坐标)由第一win.checkMouse()
在while
循环逮住点击两次。实施例而不sleep()
from graphics import *
win = GraphWin("test", 410, 505)
rectangle = Rectangle(Point(100, 100), Point(300, 300))
rectangle.draw(win)
while True:
coordinate = win.checkMouse()
if coordinate:
print("coordinate:", coordinate)
break
win.close()
编辑::第二次点击将由coordinate = win.checkMouse()
from graphics import *
import time
win = GraphWin("test", 410, 505)
while not win.checkMouse():
rectangle = Rectangle(Point(100, 100), Point(300, 300))
rectangle.draw(win)
rectangle.undraw()
# time for second click
time.sleep(2)
coordinate = win.checkMouse()
print("coordinate:", coordinate)
win.close()
EDIT被获取结合功能到鼠标按钮
from graphics import *
# --- functions ---
def on_click_left_button(event):
x = event.x
y = event.y
rectangle = Rectangle(Point(x, y), Point(x+100, y+100))
rectangle.draw(win)
def on_click_right_button(event):
win.close()
win.quit()
# --- main ---
win = GraphWin("test", 410, 505)
win.bind('<Button-1>', on_click_left_button)
win.bind('<Button-3>', on_click_right_button)
win.mainloop()
你在第一个'win.checkMouse()中忘记了'()'' – furas