通过交互式鼠标点调用 Python Zelle 图形 drawLine()

Invoke Python Zelle graphics drawLine() via interactively mousing points

https://www.cs.swarthmore.edu/~newhall/cs21/pythondocs/using-graphics.html

我正在尝试创建一个 Python Zelle 图形函数,允许用户使用鼠标单击 his/her 选择的两个点来绘制一条线。这是我目前所拥有的:

def drawLine():
    win = GraphWin("Window", 250, 250)

    p = win.getMouse()
    line = Line((p.getX, p.getY), (p.getX, p.getY))
    line.setOutline("black")
    line.draw(win)

解决了所以我认为我应该 post 代码。

def drawLine():
    win = GraphWin("Window", 250, 250)

    p = win.getMouse()
    p2 = win.getMouse()

    line = Line(Point(p.getX(), p.getY()), Point(p2.getX(), p2.getY()))
    line.setOutline("black")
    line.draw(win)

最好在用户放置第二个点时就他们放置第一个点的位置向用户提供反馈:

from graphics import *

def drawLine(window):

    pt1 = window.getMouse()
    pt1.draw(window)

    pt2 = window.getMouse()

    line = Line(pt1, pt2)
    line.setOutline("black")
    line.draw(window)

    pt1.undraw()

win = GraphWin("Window", 250, 250)

drawLine(win)

win.getMouse()
win.close()