简体   繁体   English

Python graphics.py。 如何获得checkMouse()的返回?

[英]Python graphics.py. How to get the return of checkMouse()?

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()

The coordinate keeps printing None. 坐标继续打印无。 How can I get the coordinates of the win.checkMouse() when the window has been pressed? 按下窗口后,如何获取win.checkMouse()的坐标?

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

Try this. 尝试这个。

checkMouse() function returns last mouse click or None if mouse has not been clicked since last call. checkMouse()函数返回上一次鼠标单击,如果自上次调用以来未单击鼠标,则返回None。 So it makes clicked value to None while exiting the while loop. 因此,它在退出while循环时将clicked值设置为None。

You forgot () in first win.checkMouse() 您在第一次win.checkMouse()忘记了()

In your example you have to click twice because first click (and coordinates) is catched by first win.checkMouse() in while loop. 在您的示例中,您必须单击两次,因为在while循环中,第一个win.checkMouse()了第一次单击(和坐标)。 Second click will be catched by coordinate = win.checkMouse() 第二次点击将被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: Example without sleep() 编辑:没有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: binding function to mouse buttons 编辑:绑定功能到鼠标按钮

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM