()
あなたは最初に忘れたwin.checkMouse()
あなたの例では、最初のクリック(および座標)が最初win.checkMouse()
のwhile
ループでキャッチされるため、2回クリックする必要があります。2 回目のクリックは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()
編集:なしの例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()
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()