我很感谢大家在这方面的帮助,因为我是Python的新手。下面的脚本将用鼠标单击3次来绘制一个三角形。我想用一个循环来改变这个脚本,允许无限的鼠标点击。下一步有人能帮我吗?脚本下面提供了我想要它做什么的图片。
import graphics as g
def main():
win=g.GraphWin("Draw a Triangle")
win.setCoords(0.0,0.0,100.0,100.0)
message=g.Text(g.Point(50,50),"Click on three points")
message.draw(win)
p1=win.getMouse()
p1.draw(win)
p2=win.getMouse()
p2.draw(win)
p3=win.getMouse()
p3.draw(win)
triangle=g.Polygon(p1,p2,p3)
triangle.setFill("Red")
triangle.setOutline("cyan")
triangle.draw(win)
message.setText("click anywhere to quit.")
win.getMouse()
main()
print(main)
下面是我希望它做的事情。在我的第二个鼠标点击它会自动绘制点和一条线之间的第一个和第二个点。然后,对于点3,点4也是如此,对于无限的点,etc.with选项。
发布于 2018-06-04 05:13:47
其主要思想是跟踪用户单击的点,并在重新绘制对象时清除画布。
您使用的graphics.py
文件似乎来自here。反过来,代码只是TkInter的包装器。
因此,只要熟悉一下TkInter是如何处理鼠标和按键的,你就应该能够做你想做的事情了。例如,与使用win.getMouse()
相比,使用图形用户界面框架的首选方法是将函数(事件处理程序)绑定到特定事件,如下所示。
import graphics as g
def main():
win = g.GraphWin("Draw a polygon")
win.setCoords(0.0, 0.0, 100.0, 100.0)
message = g.Text(g.Point(50, 50), "Click to add point to polygon")
message.draw(win)
# Set of points clicked so far
points = []
def onClick(pt):
x, y = win.toWorld(pt.x, pt.y)
points.append(g.Point(x, y))
poly = g.Polygon(*points)
# Clear all objects on canvas.
# You can choose to delete only current polygon by associating a label with it.
# See Tkinter's documentation for details
win.delete("all")
poly.setFill("Red")
poly.setOutline("Cyan")
poly.draw(win)
win.setMouseHandler(onClick)
# This is not idea as we are wasting cycles doing polling
# I believe Tkinter should have a better approach to avoid this.
while not win.checkKey() == 'q':
pass
if __name__ == "__main__":
main()
https://stackoverflow.com/questions/50670644
复制相似问题