Hi guys I am looking to create the following as shown below. Anybody able to give me a hand as im really struggling to make the rectangles different lengths.

Attachments patch1.png 57.41 KB

Red rectangles looks same size for me and spacing is also constant.
Role of spacing rectangles and red rectangles only alternate with spacing white ones. 3 white, 3 red, 3 white ....

Swap the spacing and rectangles after each line:

rect, spacing = spacing, rect

Edited 6 Years Ago by pyTony: n/a

I don't have a lot of time, but here is a simple example to start you, using Tkinter. The other GUI toolkits, WxPython and QT, would be similar.

from Tkinter import *

root = Tk()
root.title('Rectangle Test')
canvas = Canvas(root, width =300, height=300)

## top_left_x, top_left_y, bottom_right_x, bottom_right_y
points_list = [0, 0, 25, 25]
color = "red"

for row in range(3):
    for col in range(3):
        rectang = canvas.create_rectangle(points_list[0], points_list[1], \
                              points_list[2], points_list[3], fill=color)
        ## increase along the x axis
        for point in [0, 2]:
            points_list[point] += 50

    ## move down one row
    if row%2:      ## row is an odd number
        points_list = [ 0, points_list[1]+25, 25, points_list[3]+25]
    else:          ## row is an even number
        points_list = [25, points_list[1]+25, 50, points_list[3]+25]

canvas.pack()
root.mainloop()

Cheers for your help guys. Not sure what Tkinter is as i am using the graphics module anybody have any help using the graphics module.

The middle rectangle, be it red or white is centered, so if you have a 200x200 canvas, the middle would be 100. You can then move left or right, calculating the end position of the first red square for the rows with 4 red squares; end position=middle - (1.5 white + 1 red). The other row is obviously rectangles of the same size.

Cheers for your help guys. Not sure what Tkinter is as i am using the graphics module anybody have any help using the graphics module.

John Zelle, Ph.D. teaches Python at Wartburg College
He is the author of graphics.py used by a number of schools instead
of Tkinter


Get the graphics module (a thin wrapper for Tkinter) here ...
http://mcsp.wartburg.edu/zelle/python/graphics.py

also has a nice documentation ...
http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

Edited 5 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.