2016-05-16 3 views
0

ユーザがウィンドウ内でクリックするプログラムを作成しようとしており、これによってウィンドウに描画されたストアドポイントのリストが作成されます。ユーザーは何度でもクリックすることができますが、左下の「完了」と書かれている矩形内をクリックすると、リストが完成します。graphics.pyウィンドウでマウスクリックからプロットされたポイントのリストを作成する

「完了」をクリックするまで、ポイントをプロットできるループを作成することに固執しました。ここで

は、私は(私は多くのことを行方不明です知っている)、これまで持っているものです。

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

グラフィックウィンドウ内でユーザがクリックから保存された点のリストを作成するための最良の方法は何ですか?私は後でこれらの点を使用する予定ですが、最初にポイントを保存したいだけです。

+0

広すぎるとの意見(現在の状態)ベースを...ただ、マウスの移動/ダウン/アップイベントを作成して、どこかのリストを格納します。私はあなたのコードでそれを見ていません。代わりにあなたは良い考えではないループでポーリングしています。作業している言語/ IDEタグとOSタグを追加します。私はこれを見てみることを強くお勧めします:[C++/VCL/GDIの単純なドラッグアンドドロップの例](http://stackoverflow.com/a/20924609/2521214)私の簡単な例(フルプロジェクト+ exe zipファイルが含まれています)では、画面上のオブジェクトのいくつかの種類は、それらを移動し、それらを削除... – Spektre

答えて

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() 
関連する問題