2016-05-16 65 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()