[英]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()
坐标继续打印无。 按下窗口后,如何获取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
尝试这个。
checkMouse()函数返回上一次鼠标单击,如果自上次调用以来未单击鼠标,则返回None。 因此,它在退出while循环时将clicked值设置为None。
您在第一次win.checkMouse()
忘记了()
。
在您的示例中,您必须单击两次,因为在while
循环中,第一个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()
编辑:没有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()
编辑:绑定功能到鼠标按钮
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.