I'm a new user of Python and I have one problem. I'm just painting rectangles in Python and I must fit these 5 commands:
canvas.create_rectangle(90, 90, 150, 150, fill='yellow')
canvas.create_rectangle(150, 90, 210, 150, fill='red')
canvas.create_rectangle(90, 150, 150, 210, fill='green')
canvas.create_rectangle(30, 90, 90, 150, fill='red')
canvas.create_rectangle(90, 30, 150, 90, fill='green')
in 3 commands and I don't know how to do that. What would be a way to shorten this code?
CodePudding user response:
Something like this?
rectangles = (
(90, 90, 150, 150, 'yellow'),
(150, 90, 210, 150, 'red'),
(90, 150, 150, 210, 'green'),
(30, 90, 90, 150, 'red'),
(90, 30, 150, 90, 'green'))
for x1, y1, x2, y2, color in rectangles:
canvas.create_rectangle(x1, y1, x2, y2, fill=color)
CodePudding user response:
You can make the red and green longer rectangles and paint them on top of each other with yellow going last:
canvas.create_rectangle(30, 90, 210, 150, fill='red')
canvas.create_rectangle(90, 30, 150, 210, fill='green')
canvas.create_rectangle(90, 90, 150, 150, fill='yellow')