2016-11-17 82 views
0
win=GraphWin("test",410,505) 

while win.checkMouse==None: 
    rectangle=Rectangle(Point(100,100),Point(300,300)) 
    rectangle.draw(win) 
    rectangle.undraw() 
coordinate=win.checkMouse() 

坐标继续打印无。如何在窗口被按下时获得win.checkMouse()的坐标?Python graphics.py。如何获得checkMouse()的回报?

+0

你在第一个'win.checkMouse()中忘记了'()'' – furas

回答

1
win=GraphWin("test",410,505) 

coordinate = win.checkMouse() 
while coordinate == None: 
    rectangle=Rectangle(Point(100,100),Point(300,300)) 
    rectangle.draw(win) 
    rectangle.undraw() 
    coordinate = win.checkMouse() 
print coordinate 

试试这个。

如果鼠标自上次调用后未被点击,checkMouse()函数将返回最后一次鼠标单击或无。所以在退出while循环时,它会将值单击为None。

0

您在第一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()