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

时间:2018-10-18 19:11:17

标签: python zelle-graphics

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

我正在尝试创建一个Python Zelle图形函数,该函数将允许用户使用鼠标单击他/她选择的两个点以画一条线。这是我到目前为止的内容:

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)

2 个答案:

答案 0 :(得分:0)

解决了这个问题,以为我应该发布代码。

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)

答案 1 :(得分:0)

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

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