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 に答える