Create this image with the graphics.py module

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AETH3Rrr
    New Member
    • Jan 2021
    • 1

    Create this image with the graphics.py module

    I want to create this shape but with a loop so it shortens down my code. This is using the Graphics.py module

    Can anyone help pls

    from graphics import *

    def drawPatchLines( ):
    win = GraphWin("Windo w",100,100)
    win.setBackgrou nd('white')

    #Draws the white rectangle in top left
    rect = Rectangle(Point (0,0), Point(100,100))
    rect.setOutline ('black')
    rect.setFill('w hite')
    rect.draw(win)

    #Draws the red lines
    poly = Polygon (Point(0,100), Point(10,100), Point(10,90), Point(0,90))
    poly.setFill('r ed')
    poly.setOutline ('red')
    poly.draw(win)

    poly1 = Polygon (Point(20,100), Point(30,100), Point(30,70), Point(0,70), Point(0,80), Point(20,80))
    poly1.setFill(' red')
    poly1.setOutlin e('red')
    poly1.draw(win)

    poly2 = Polygon (Point(40,100), Point(50,100), Point(50,50), Point(0,50), Point(0,60), Point(40,60))
    poly2.setFill(' red')
    poly2.setOutlin e('red')
    poly2.draw(win)

    poly3 = Polygon (Point(60,100), Point(70,100), Point(70,30), Point(0,30), Point(0,40), Point(60,40))
    poly3.setFill(' red')
    poly3.setOutlin e('red')
    poly3.draw(win)

    poly4 = Polygon (Point(80,100), Point(90,100), Point(90,10), Point(0,10), Point(0,20), Point(80,20))
    poly4.setFill(' red')
    poly4.setOutlin e('red')
    poly4.draw(win)
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    If OS is Windows, get the graphics.py module as follows.
    C:¥>pip install graphics.py-extra==0.0.4

    Change the code as follows.
    Code:
    from graphics import *
    
    def drawPatchLines():
        win = GraphWin("Window",100,100)
        win.setBackground('white')
    
        #Draws the white rectangle in top left
        rect = Rectangle(Point(0,0), Point(100,100))
        rect.setOutline('black')
        rect.setFill('white')
        rect.draw(win)
    
        #Draws the red lines
        for i in range(0, 100, 20):
            poly = Polygon (Point(i,100), Point(i+10,100), Point(i+10,90-i), Point(0,90-i), Point(0,100-i), Point(i,100-i))
            poly.setFill('red')
            poly.setOutline('red')
            poly.draw(win)
    
        win.getMouse()
        win.close()
    
    drawPatchLines()

    Comment

    Working...