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