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

坐標繼續打印無。 按下窗口后,如何獲取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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM