# GUI Application development using graphics,py

```http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html
https://www.pas.rochester.edu/~rsarkis/csc161/python/graphics.html
https://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf
https://www.cs.swarthmore.edu/~richardw/classes/cs21/s13/using-graphics.html
It’s a simple object oriented graphics library.
The library is designed to make it very easy for novice programmers to experiment
with computer graphics in an object oriented fashion. It is written by John Zelle for
use with the book &quot;Python Programming: An Introduction to Computer Science&quot;
(Franklin, Beedle &amp; Associates).
The GraphWin class implements a window where drawing can be done, and
various graphics objects are provided that can be drawn into a GraphWin.
The library provides the following graphical objects:
Point
Line
Circle
Oval
Rectangle
Polygon
Text
Entry (for text-based input)
Image
Functions available in this library:
















close()
flush()
# update drawing to graphics window
getHeight()
getWidth()
getMouse() # wait for mouse click and return Point obj representing click
location
setBackground(color)
# color could be something like &quot;red&quot;
setCoords(xll, yll, xur, yur) # Sets the coordinate system of the window so
that the lower left corner is at (xll, yll) and the upper right corner is at (xur, yur)
Point(x, y)
clone()
# create a new Point object that is an exact copy of this one
getX()
# the int value of the Point's x-coordinate
getY()
# the int value of the Point's y-coordinate
draw(graphwin):
# Draw the object in graphwin, which should be a
GraphWin object. A GraphicsObject may only be drawn into one window.
move(dx, dy):
# move object dx units in x and dy units in y direction
setFill(color):
# Set interior color to color
setOutline(color):
# Set outline color to color
setWidth(width):
# Set line weight to width














undraw():
# Undraw the object (i.e. hide it).
Line(p1, p2)
# p1 and p2 are the end Points of the line
Rectangle( p1, p2) # p1 and p2 are points for upper left lower rt
# p1 is a Point at the center of the circle, radius is an int
Oval(p1, p2)
# p1 and p2 are Points of opposite corners of
bounding rectangle
Polygon()
# ex. triangle: Polygon ([Point(10,20), Point(30,40),
Point(10,60)])
getPoints()
# return a list of Points in the polygon
text(p, text)
# p is center point and text is string
getText()
setText(text)
setTextColor(color)
setFace(family)
# set font face, ex. &quot;arial&quot; &quot;courier&quot;
setSize(size)
# set font size (5-36 are legal)
setStyle(style)
# set font style ex. &quot;bold&quot; &quot;italic&quot;
Design an interactive program to draw Basic shapes using graphics library identified in Lab
Exercise2
from graphics import *
win = GraphWin(&quot;My GUI Window&quot;, 500, 500)
win.setBackground(&quot;pink&quot;)
#Point
pt = Point(50, 50) # set the coordinates of point
pt.setOutline(&quot;blue&quot;) #set the color of the point
pt.draw(win)
# draw the point
#Line
pt1 = Point(50,100)
pt2 = Point(200,100)
ln = Line(pt1,pt2)
ln.setOutline(color_rgb(0,255,255))
ln.setWidth(5)
ln.draw(win)
#Rectangle
rect = Rectangle(Point(50,140),Point(200,200))
rect.setOutline(&quot;black&quot;)
rect.setFill(color_rgb(255,100,50))
rect.setWidth(3)
rect.draw(win)
#Circle
circ = Circle(Point(100,300), 50) # set the coordinates of circle
circ.setFill(&quot;purple&quot;)# set the color of circle
circ.draw(win)
# draw the circle
#Polygon
poly = Polygon(Point(50,450),Point(100,370),Point(200,450))
poly.setFill(color_rgb(255,0,255))
poly.draw(win)
#Oval
ov = Oval(Point(300, 50),Point(400,100))
ov.setFill(color_rgb(255,0,0))
ov.draw(win)
Draw Hut, Fish, Scenary and UseCase Diagram of any one software application using using
graphics library identified in Lab Exercise 2
from graphics import *
win = GraphWin(&quot;My GUI Window&quot;, 500, 500)
win.setBackground(&quot;light blue&quot;)
Hut
#Rectangles
rect1 = Rectangle(Point(20,110),Point(120,300))
rect1.setOutline(&quot;black&quot;)
rect1.setFill(&quot;yellow&quot;)
rect1.setWidth(3)
rect1.draw(win)
rect2 = Rectangle(Point(120,110),Point(420,300))
rect2.setOutline(&quot;black&quot;)
rect2.setFill(color_rgb(255,100,50))
rect2.setWidth(3)
rect2.draw(win)
rect3 = Rectangle(Point(110,200),Point(30,300))
rect3.setOutline(&quot;black&quot;)
rect3.setFill(&quot;brown&quot;)
rect3.setWidth(3)
rect3.draw(win)
#Polygon
poly1 = Polygon(Point(70,10),Point(120,110),Point(20,110))
poly1.setFill(&quot;blue&quot;)
poly1.draw(win)
poly2 = Polygon(Point(70,10),Point(120,110),Point(420,110), Point(370,10))
poly2.setFill(&quot;purple&quot;)
poly2.draw(win)
Fish
#fish body
ov = Oval(Point(100, 370),Point(300,450))
ov.setFill(&quot;orange&quot;)
ov.draw(win)
#fish eye
circ = Circle(Point(150,390), 5)
circ.setFill(&quot;purple&quot;)
circ.draw(win)
#fish tail
poly3 = Polygon(Point(300,410),Point(350,360),Point(350,460))
poly3.setFill(&quot;blue&quot;)
poly3.draw(win)
#fish mouth
poly4 = Polygon(Point(100,410),Point(120,410),Point(100,430))
poly4.setFill(&quot;light blue&quot;)
poly4.setOutline(&quot;light blue&quot;)
poly4.draw(win)
#fins
poly3 = Polygon(Point(180,370),Point(230,340),Point(230,370))
poly3.setFill(&quot;blue&quot;)
poly3.draw(win)
poly3 = Polygon(Point(180,450),Point(230,480),Point(230,450))
poly3.setFill(&quot;blue&quot;)
poly3.draw(win)
Scenery
from graphics import *
win = GraphWin(&quot;My GUI Window&quot;, 500, 500)
win.setBackground(&quot;brown&quot;)
#grass
rect = Rectangle(Point(20,400),Point(480,480))
rect.setOutline(&quot;green&quot;)
rect.setFill(&quot;green&quot;)
rect.draw(win)
rect = Rectangle(Point(20,20),Point(480,400))
rect.setOutline(&quot;light blue&quot;)
rect.setFill(&quot;light blue&quot;)
rect.draw(win)
#Mountains
poly1 = Polygon(Point(230,400),Point(20,400),Point(115,200))
poly1.setFill(color_rgb(101,67,33))
poly1.draw(win)
poly = Polygon(Point(230,400),Point(480,400),Point(345,200))
poly.setFill(color_rgb(101,67,33))
poly.draw(win)
#sun
circ = Circle(Point(400,120), 60)
circ.setFill(&quot;yellow&quot;)
circ.draw(win)
UseCase Diagram of any one software application
from graphics import *
win = GraphWin(&quot;Use case diagram of Library management system&quot;, 500, 500)
win.setBackground(&quot;black&quot;)
#Boundary of system
rect = Rectangle(Point(150,50),Point(350,400))
rect.setFill(&quot;white&quot;)
rect.setWidth(3)
rect.draw(win)
#Use Cases
ov = Oval(Point(200, 100),Point(300,150))
ov.setFill(&quot;grey&quot;)
ov.draw(win)
ov = Oval(Point(200, 200),Point(300,250))
ov.setFill(&quot;grey&quot;)
ov.draw(win)
ov = Oval(Point(200, 300),Point(300,350))
ov.setFill(&quot;grey&quot;)
ov.draw(win)
#Actors
circ = Circle(Point(50,200), 20)
circ.setFill(&quot;white&quot;)
circ.draw(win)
circ = Circle(Point(450,200), 20)
circ.setFill(&quot;white&quot;)
circ.draw(win)
ln = Line(Point(50,200),Point(50,270))
ln.setOutline(&quot;white&quot;)
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(450,200),Point(450,270))
ln.setOutline(&quot;white&quot;)
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(30,240),Point(70,240))
ln.setOutline(&quot;white&quot;)
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(430,240),Point(470,240))
ln.setOutline(&quot;white&quot;)
ln.setWidth(5)
ln.draw(win)
#Relationships
ln = Line(Point(70,200),Point(220,140))
ln.setOutline(&quot;grey&quot;)
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(70,200),Point(210,320))
ln.setOutline(&quot;grey&quot;)
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(450,220),Point(300,220))
ln.setOutline(&quot;grey&quot;)
ln.setWidth(5)
ln.draw(win)
#Text
txt=Text(Point(250,120),&quot;Borrow book&quot;)
txt.setTextColor(&quot;white&quot;)
txt.draw(win)
txt=Text(Point(250,220),&quot;Issue book&quot;)
txt.setTextColor(&quot;white&quot;)
txt.draw(win)
txt=Text(Point(250,330),&quot;Return book&quot;)
txt.setTextColor(&quot;white&quot;)
txt.draw(win)
Draw a scenery and show moving boat,rising sun, raining and fish swimming in water
from graphics import *
import time
def moveAll(shapeList, dx, dy):
''' Move all shapes in shapeList by (dx, dy).'''
for shape in shapeList:
shape.move(dx, dy)
def moveAllOnLine(shapeList, dx, dy, repetitions, delay):
'''Animate the shapes in shapeList along a line.
Move by (dx, dy) each time.
Repeat the specified number of repetitions.
Have the specified delay (in seconds) after each repeat.
'''
for i in range(repetitions):
moveAll(shapeList, dx, dy)
time.sleep(delay)
def main():
win = GraphWin(&quot;My GUI Window&quot;, 500, 500)
#Water
rect = Rectangle(Point(20,300),Point(480,480))
rect.setOutline(&quot;blue&quot;)
rect.setFill(&quot;blue&quot;)
rect.draw(win)
#Sky
rect = Rectangle(Point(20,20),Point(480,300))
rect.setOutline(&quot;light blue&quot;)
rect.setFill(&quot;light blue&quot;)
rect.draw(win)
#Mountains
poly1 = Polygon(Point(230,300),Point(20,300),Point(115,200))
poly1.setFill(color_rgb(101,67,33))
poly1.draw(win)
poly = Polygon(Point(230,300),Point(480,300),Point(345,200))
poly.setFill(color_rgb(101,67,33))
poly.draw(win)
#Raindrops
for j in range(3): #for 3 loops of raindrops
drops=[]
#list to store all the rain drops
for i in range(0,10):
circ = Circle(Point(50+40*i,0), 10)
circ.setFill(color_rgb(30,144,255))
circ.draw(win)
drops.append(circ)
moveAllOnLine(drops, 0, 6, 60, .01) # function for raindrops to fall
for i in drops:
i.undraw() #Undraw the object (i.e. hide it).
#Sun
circ = Circle(Point(0,200), 30)
circ.setFill(&quot;yellow&quot;)
circ.draw(win)
moveAllOnLine([circ], 8, -3, 46, .05)
#Boat
boat = Polygon(Point(100,330),Point(200,330),Point(180,370),Point(120,370))
boat.setFill(&quot;brown&quot;)
boat.draw(win)
#fish
#fish body
body = Oval(Point(120, 400),Point(250,450))
body.setFill(&quot;orange&quot;)
body.draw(win)
#fish eye
eye = Circle(Point(140,420), 5)
eye.setFill(&quot;purple&quot;)
eye.draw(win)
#fish tail
tail = Polygon(Point(250,422.5),Point(300,407.5),Point(300,437.5))
tail.setFill(&quot;red&quot;)
tail.draw(win)
movement = [body,eye,tail,boat] #list of objects that are moving together
moveAllOnLine(movement, 5, 0, 46, .05) #function for movement in right dirn
moveAllOnLine(movement, -5, 0, 46, .05) #function for movement in left dirn
win.getMouse()
win.close()
main()
Draw solar system
from graphics import *
win = GraphWin(&quot;My GUI Window&quot;, 500, 500)
win.setBackground(&quot;black&quot;)
#Sun in the middle of Solar system
circ = Circle(Point(235,235), 25)
circ.setFill(&quot;yellow&quot;)
circ.draw(win)
#Orbits of planets
#orbit of mercury
ov = Oval(Point(170, 200),Point(300,270))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of venus
ov = Oval(Point(140, 190),Point(330,280))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of earth
ov = Oval(Point(110, 170),Point(365,295))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of mars
ov = Oval(Point(90, 150),Point(395,315))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of jupiter
ov = Oval(Point(70, 130),Point(420,325))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of saturn
ov = Oval(Point(50, 110),Point(435,340))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of uranus
ov = Oval(Point(35, 90),Point(460,360))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#orbit of neptune
ov = Oval(Point(20, 70),Point(480,380))
ov.setOutline(&quot;white&quot;)
ov.draw(win)
#Planets
#Mercury
circ = Circle(Point(260,265), 5)
circ.setFill(color_rgb(255,235,205))
circ.draw(win)
#venus
circ = Circle(Point(180,270), 6)
circ.setFill(&quot;orange&quot;)
circ.draw(win)
#Earth
circ = Circle(Point(140,190), 9)
circ.setFill(&quot;light green&quot;)
circ.draw(win)
#Mars
circ = Circle(Point(290,155), 7)
circ.setFill(&quot;red&quot;)
circ.draw(win)
#Jupiter
circ = Circle(Point(410,250), 18)
circ.setFill(color_rgb(244,164,96))
circ.draw(win)
#Saturn
circ = Circle(Point(310,330), 15)
circ.setFill(color_rgb(255,192,203))
circ.draw(win)
#Uranus
circ = Circle(Point(160,350), 12)
circ.setFill(color_rgb(135,206,250))
circ.draw(win)
#Neptune
circ = Circle(Point(50,300), 10)
circ.setFill(color_rgb(100,149,237))
circ.draw(win)
```