Below is a Python program I wrote to draw lines as many al there are mouse clicks. However, I wanted all the line segments to have the same attributes (eg: color, width etc), but that's not happening - only the last segment has my desired attributes. I am a beginner & need help please. Thanks!
from graphics import *
def isBetween(x, end1, end2):
The ends do not need to be in increasing order.'''
return end1 <= x <= end2 or end2 <= x <= end1
def isInside(point, rect):
'''Return True if the point is inside the Rectangle rect.'''
pt1 = rect.getP1()
pt2 = rect.getP2()
return isBetween(point.getX(), pt1.getX(), pt2.getX()) and \
isBetween(point.getY(), pt1.getY(), pt2.getY())
def polyHere(rect, win):
''' Draw a polygon interactively in Rectangle rect, in GraphWin win.
Collect mouse clicks inside rect into a Polygon.
When a click goes outside rect, stop and return the final polygon.
The polygon ends up drawn. The method draws and undraws rect.'''
rect.setOutline("red")
rect.draw(win)
vertices = list()
pt2 = win.getMouse()
vertices.append(pt2)
pt = win.getMouse()
n = 0
while isInside(pt, rect):
vertices.append(pt)
poly = Polygon(vertices)
poly.draw(win)
del vertices[0]
pt = win.getMouse()
rect.undraw()
return poly
def main():
winWidth = 400
winHeight = 400
win = GraphWin('Drawing Polygons', winWidth, winHeight)
win.setCoords(0, 0, winWidth, winHeight)
instructions = Text(Point(winWidth/2, 30),
"Click vertices inside the red rectangle."+
"\nClick outside the rectangle to stop.")
instructions.draw(win)
rect1 = Rectangle(Point(5, 55), Point(200, 120))
poly1 = polyHere(rect1, win)
poly1.setOutline('green')
poly1.setWidth(2)
rect2 = Rectangle(Point(210, 50), Point(350, 350))
poly2 = polyHere(rect2, win)
poly2.setOutline('blue')
poly2.setWidth(3)
instructions.setText("Click anywhere to quit.")
win.getMouse()
win.close()
main()
Emmanuel_2 0 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.