Graphics module (graphics.py) contains a number of graphics functions. They can be used to create different pattern images. In this post we will discuss Python Line, Circle and Polygon classes defined in graphics.py. We will create four patterns using these objects of four classes.
Point
The Graphics point object is created by passing X and Y coordinate values to the constructor (class name with parenthesis). X and Y are integer values representing x-coordinate and y-coordinate values to create a point in the drawing area. When Draw method is called with the point object, a point is displayed in the drawing area with current color. It can be declared as
obj_name= Point( X, Y)
startPt= Point(100,100) # picks the point at coordinates X=100 and Y=100 and create a point object
Line
Python line object is created by passing two point objects to the constructor of Line class. The two point object arguments represent two end points of the line. When Draw method is called with the line object, a line is displayed starting from the first point to the second point passed as arguments. It can be declared as
obj_name= Line( Point(X1, Y1), Point(X2,Y2))
objLine= Line(Point(100,100), Point(200,200))
or
p1=Point(X1, Y1)
P2=Point(X2,Y2)
obj_name= Line( p1,p2)
p1=Point(100,100) P2=Point(200,200) objLine = Line( p1,p2)
Circle
The Graphics circle object is created by passing one point object as center of the circle and radius as positive integer to the constructor of Circle class. When Draw method is called with the declared circle object, a circle is created at center passed as first argument a point object and second as radius. It can be declared as
obj_name= Circle( Point(X1, Y1), radius)
objCircle= Circle(Point(50,50), 20) # a circle of radius 20 pixels will be created and center is at X=50, Y=50
Polygon
The Graphics polygon object is created by passing a list of points as argument to the constructor of Circle class. The number of points must be equal to or greater than three. A list of three points creates a triangle, a list of four points creates a square, a list of five points create pentagon and so on. When Draw method is called with the declared polygon object, it gets the length of list argument and creates a polygon out of those points. It can be declared as
obj_name= Polygon( list of points)
listOfPoints= [Point(100,100),Point(50,200),Point(200,250)] objCircle= Polygon(listOfPoints) # a triangle will be created with vertices at (100,100), (50,200) and (200,250)
Create pattern of multiple colored lines.
import random from graphics import * def main(): col_arr=["violet","indigo","blue","green", "yellow","orange","red","pink", "brown","purple","gray","maroon", "black"]# create list of colors for setting line outlines workArea = GraphWin('Random Lines', 300, 300) # give title and dimensions i=0 while i<100: randx1=random.randrange(300)# x of first end point randy1=random.randrange(300)# y of first end point randx2=random.randrange(300)# x of second end point randy2=random.randrange(300)# y of second end point rline=Line(Point(randx1, randy1), Point(randx2, randy2))#create line object rline.setOutline(col_arr[i%13]) #change color rline.draw(workArea)# draw line i+=1 message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit') message.draw(workArea)#print text message on screen workArea.getMouse()# get mouse to click on screen to exit workArea.close() # close the workArea window main()
Create a pattern of lines of different colors and lengths starting from left end of drawing area
import random import time from graphics import * def main(): #create list of colors col_arr=["violet","indigo","blue","green", "yellow","orange","red","pink", "brown","purple","gray","maroon", "black"] workArea = GraphWin('Horizontal Lines Pattern', 300, 300) # give title and dimensions i=0 while i<1000: x2=random.randrange(300)#generate random point y1=random.randrange(300) rline=Line(Point(1, y1), Point(x2, y1))#draw horizontal line from left i rline.setOutline(col_arr[i%13]) #change color rline.draw(workArea)#draw line time.sleep(0.01) i+=1 message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit') message.draw(workArea) workArea.getMouse()# get mouse to click on screen to exit workArea.close() # close the workArea window main()
Create a pattern with circles of different colors, radii and outline widths
import random import time from graphics import * def main(): # generate list of colors col_arr=["violet","indigo","blue","green", "yellow","orange","red","pink", "brown","purple","gray","maroon", "black"] workArea = GraphWin('Multiple Circle Pattern', 300, 300) # give title and dimensions i=0 while i<500: x1=random.randrange(300)#Generate random center points y1=random.randrange(300) rcir=Circle(Point(x1, y1), i%40)# create circle object with radius depending on current counter rcir.setOutline(col_arr[i%13]) #change color rcir.setWidth(i%5)#change width of outline rcir.draw(workArea)#draw circles at genereated center time.sleep(0.01) i+=1 message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit') message.draw(workArea) workArea.getMouse()# get mouse to click on screen to exit workArea.close() # close the workArea window main()
Create a pattern of different triangles of different colors and outline widths
import random import time from graphics import * def main(): workArea = GraphWin('Colored Triangles Pattern', 300, 300) # give title and dimensions col_arr=["violet","indigo","blue","green", "yellow","orange","red","pink", "brown","purple","gray","maroon", "black"] # set of colors to be used for setting outline colors i=0 while i<20: x1=random.randrange(300) y1=random.randrange(300) x2=random.randrange(300) y2=random.randrange(300) x3=random.randrange(300) y3=random.randrange(300)#generate random coordiates for triangle ptList=[Point(x1,y1),Point(x2,y2),Point(x3,y3)] #create list of points rpoly=Polygon(ptList)# create the polgon object rpoly.setOutline(col_arr[i%13]) #change color of triangle rpoly.setWidth((i+1)/4)#change width of outline rpoly.draw(workArea)# draw the triangle time.sleep(0.01)# delay by 100th of a second to create animated effect i+=1 # increment loop counter message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit') message.draw(workArea) workArea.getMouse()# get mouse to click on screen to exit workArea.close() # close the workArea window main()