0

I am trying to create a program where the user clicks in a window, and this creates a list of stored points that are also drawn in the window. The user can click as many times as they want, but once they click within the rectangle that says "Done" in the lower left, the list is complete.

I have gotten stuck on creating the loop that allows the user to plot the points until they click "Done".

Here is what I have so far (I know I am missing a lot):

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

What is the best way to create a list of stored points from the user clicking within the graphic window? I plan to use these points later, but I just want to get the points stored first.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Too broad and opinion based (in current state)... Just create on mouse move/down/up events and store the list somewhere. I do not see any of it in your code. instead you are poling in a loop that is not a good idea. Add language/IDE and OS tags you are working with. I strongly recommend to look at this: [simple Drag&Drop example in C++/VCL/GDI](http://stackoverflow.com/a/20924609/2521214) with my simple example (full project+exe zip file included) that can add few types of objects on the screen, move them, remove them ... – Spektre May 18 '16 at 07:28

1 Answers1

0

The primary problems with your code are: you're missing a loop to collect and test points; your and test to see if the user clicked in the box should be an or test; there isn't enough time for the user to see the "Thank you!" message before the window closes.

To collect your points, you can use an array and instead of the count variable, just let the array's length represent the count. Below's a rework of your code that addresses the above issues:

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81