I am creating a bar graph essentially just by only using the graphics module. I am stuck with it. Below is my code that I have done which creates a perfect square. (10 x 10) so for every one bar I was it to decrease by 10.

def drawRectanglePatch(win, x, y):
    Point(200,200)
    for i in range(10):
        for j in range(10):
               topLeftX = x + i * 10
               topLeftY = y + j * 10
               rectangle = Rectangle(Point(topLeftX, topLeftY),
                                              Point(topLeftX + 10 , topLeftY + 10 ))
               rectangle.setFill("red")
               rectangle.setOutline("red")
               rectangle.draw(win)

Thanks in advance

Ashley

Recommended Answers

All 3 Replies

Get the module graphics.py from:
http://mcsp.wartburg.edu/zelle/python/graphics.py

Here is a wintery example:

'''gr_SnowMan1.py
draw a snowman with module graphis.py (based on Tkinter)
http://mcsp.wartburg.edu/zelle/python/graphics.py
newer version works with Python2 and Python3
'''

from graphics import *

def main():
    # drawing canvas/window with title and size
    win = GraphWin("Snowman",400,500)

    top = Circle(Point(200,115), 45)
    top.setFill("white")
    top.setOutline("black")
    top.draw(win)


    middle = Circle(Point(200,210), 65)
    middle.setFill("white")
    middle.setOutline("black")
    middle.draw(win)

    bottom = Circle(Point(200,345), 85)
    bottom.setFill("white")
    bottom.setOutline("black")
    bottom.draw(win)

    hat = Rectangle(Point(290,150),Point(200,160))
    hat.setFill("black")
    hat.move(-43,-86)
    hat.draw(win)

    tophat = Rectangle(Point(30,30),Point(70,70))
    tophat.setFill("black")
    tophat.move(150,-7)
    tophat.draw(win)

    eye = Circle(Point(180,100), 3.5)
    eye.setFill("black")
    eye.draw(win)

    eye2=eye.clone()
    eye2.move(40,0)
    eye2.draw(win)

    nose = Polygon(Point(180,90),Point(130,100),Point(180,100))
    nose.setFill("orange")
    nose.move(20,18)
    nose.draw(win)

    mouth = eye2.clone()
    mouth.move(-50,25)
    mouth.draw(win)

    mouth2 = mouth.clone()
    mouth2.move(15,10)
    mouth2.draw(win)

    mouth3 = mouth.clone()
    mouth3.move(30,15)
    mouth3.draw(win)

    mouth4 = mouth.clone()
    mouth4.move(45,10)
    mouth4.draw(win)

    mouth5 = mouth.clone()
    mouth5.move(60,0)
    mouth5.draw(win)

    button = mouth.clone()
    button.move(30,50)
    button.draw(win)

    button2 = mouth.clone()
    button2.move(30,80)
    button2.draw(win)

    button3 = mouth.clone()
    button3.move(30,110)
    button3.draw(win)

    leftarm = Line(Point(50,100),Point(125,125))
    leftarm.setFill("brown")
    leftarm.setWidth(2)
    leftarm.move(18,60)
    leftarm.draw(win)

    rightarm = Line(Point(50,-45),Point(125,-80))
    rightarm.setFill("brown")
    rightarm.setWidth(2)
    rightarm.move(205,225)
    rightarm.draw(win)

    snow = Circle(Point(100,100), 2)
    snow.setFill("white")
    snow.setOutline("blue")
    snow.draw(win)

    snow2 = snow.clone()
    snow2.move(200,100)
    snow2.draw(win)

    snow3 = snow.clone()
    snow3.move(250,150)
    snow3.draw(win)

    snow4 = snow.clone()
    snow4.move(200,200)
    snow4.draw(win)

    snow5 = snow.clone()
    snow5.move(150,50)
    snow5.draw(win)

    snow6 = snow.clone()
    snow6.move(25,100)
    snow6.draw(win)

    snow7 = snow.clone()
    snow7.move(-15,50)
    snow7.draw(win)

    snow8 = snow.clone()
    snow8.move(-60,-20)
    snow8.draw(win)

    snow9 = snow.clone()
    snow9.move(-40,-70)
    snow9.draw(win)

    snow10 = snow.clone()
    snow10.move(275,30)
    snow10.draw(win)

    snow11 = snow.clone()
    snow11.move(230,-5)
    snow11.draw(win)

    snow12 = snow.clone()
    snow12.move(190,-50)
    snow12.draw(win)

    snow13 = snow.clone()
    snow13.move(50,-70)
    snow13.draw(win)

    snow14 = snow.clone()
    snow14.move(-40,250)
    snow14.draw(win)

    snow15 = snow.clone()
    snow15.move(-70,150)
    snow15.draw(win)

    parts = [top, middle, bottom, hat, tophat, eye, eye2,
        nose, mouth, mouth2, mouth3, mouth4, mouth5,
        button, button2, button3, leftarm, rightarm]
    for i in range(6):
        p = win.getMouse()
        c = top.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        for part in parts:
            part.move(dx,dy)

    #win.getMouse()
    win.close()

main()

This will give you a general idea of how to start if I understand what you want.

def drawRectanglePatch(x, y):
    win = GraphWin("Test", 300, 300)
    for i in range(10):
        for j in range(10):
               topLeftX = x + i * 10
##               topLeftY = y + j * 10
               incr = 0
               if j-i > 0:
                   incr = j-i
               topLeftY = y + incr * 10
               rectangle = Rectangle(Point(topLeftX, topLeftY),
                                              Point(topLeftX + 10 , topLeftY + 10 ))
               rectangle.setFill("red")
               rectangle.setOutline("white")
               rectangle.draw(win)
    win.getMouse()
drawRectanglePatch(10, 10)

A more compact version of the snow program. I'm not obsessive really.

# drawing canvas/window with title and size
win = GraphWin("Snowman",400,500)

parts_is_parts = []
fill="white"
ctr = 0
for x, y, z in ((200, 115, 45), (200, 210, 65), (200, 345, 85), (180, 100, 3.5),
                (220, 100, 3.5), (170, 125, 3.5), (185, 132, 3.5), (200, 135, 3.5),
                (215, 132, 3.5), (230, 125, 3.5), (200, 175, 3.5), (200, 205, 3.5),
                (200, 235, 3.5)):
    cir = Circle(Point(x, y), z)
    cir.setFill(fill)
    cir.setOutline("black")
    cir.draw(win)
    parts_is_parts.append(cir)

    if 2 == ctr:
        fill="black"
    ctr += 1

hat = Rectangle(Point(290,150),Point(200,160))
hat.setFill("black")
hat.move(-43,-86)
hat.draw(win)
parts_is_parts.append(hat)
tophat = Rectangle(Point(30,30),Point(70,70))
tophat.setFill("black")
tophat.move(150,-7)
tophat.draw(win)
parts_is_parts.append(tophat)

nose = Polygon(Point(180,90),Point(130,100),Point(180,100))
nose.setFill("orange")
nose.move(20,18)
nose.draw(win)
parts_is_parts.append(nose)

incr = -40
for x, y in ((142, 185), (336, 145)):
    arm = Line(Point(x, y),Point(x-75, y+incr))
    arm.setFill("brown")
    arm.setWidth(3)
    arm.draw(win)
    parts_is_parts.append(arm)
    incr *= -1

snow = Circle(Point(100,100), 2)
snow.setFill("white")
snow.setOutline("blue")
snow.draw(win)
for x, y in ((-70, 150), (-60, 20), (-40, -70), (-40, 250), (-15, 50), (25, 100), 
             (50, -70), (150, 50), (190, -50), (200, 100), (200, 200), (230, -5), 
             (250, 150), (275, 30)):
    snow2 = snow.clone()
    snow2.move(x, y)
    snow2.draw(win)

for i in range(6):
    p = win.getMouse()
    c = parts_is_parts[0].getCenter()
    dx = p.getX() - c.getX()
    dy = p.getY() - c.getY()
    for part in parts_is_parts:
        part.move(dx,dy)

win.getMouse()
win.close()
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.