0

我正在尝试创建一个用户在窗口中单击的程序,这将创建一个存储点列表,这些点也绘制在窗口中。用户可以根据需要单击任意多次,但是一旦在左下角标有“完成”的矩形内单击,列表就完成了。

我一直坚持创建允许用户绘制点的循环,直到他们单击“完成”。

这是我到目前为止所拥有的(我知道我错过了很多):

from graphics import *
def main():
    plot=GraphWin("Plot Me!",400,400)
    plot.setCoords(0,0,4,4)


    button=Text(Point(.3,.2),"Done")
    button.draw(plot)
    Rectangle(Point(0,0),Point(.6,.4)).draw(plot)

    #Create as many points as the user wants and store in a list
    count=0  #This will keep track of the number of points.
    xstr=plot.getMouse()
    x=xstr.getX()
    y=xstr.getY()
    if (x>.6) and (y>.4):
        count=count+1
        xstr.draw(plot)
    else: #if they click within the "Done" rectangle, the list is complete.
        button.setText("Thank you!")


main()

通过用户在图形窗口中单击来创建存储点列表的最佳方法是什么?我计划稍后使用这些积分,但我只想先存储积分。

4

1 回答 1

0

您的代码的主要问题是:您缺少一个循环来收集和测试点;您的and测试以查看用户单击该框是否应该是一个or测试;没有足够的时间让用户看到“谢谢!” 窗口关闭前的消息。

要收集您的积分,您可以使用数组而不是count变量,只需让数组的长度代表计数。以下是解决上述问题的代码的返工:

import time
from graphics import *

box_limit = Point(0.8, 0.4)

def main():
    plot = GraphWin("Plot Me!", 400, 400)
    plot.setCoords(0, 0, 4, 4)


    button = Text(Point(box_limit.getX() / 2, box_limit.getY() / 2), "Done")
    button.draw(plot)
    Rectangle(Point(0.05, 0), box_limit).draw(plot)

    # Create as many points as the user wants and store in a list
    point = plot.getMouse()
    x, y = point.getX(), point.getY()

    points = [point]  # This will keep track of the points.

    while x > box_limit.getX() or y > box_limit.getY():
        point.draw(plot)
        point = plot.getMouse()
        x, y = point.getX(), point.getY()
        points.append(point)

    # if they click within the "Done" rectangle, the list is complete.
    button.setText("Thank you!")

    time.sleep(2)  # Give user time to read "Thank you!"
    plot.close()

    # ignore last point as it was used to exit
    print([(point.getX(), point.getY()) for point in points[:-1]])

main()
于 2017-01-08T07:12:01.270 回答