Uploaded by aayushi.bansal2001

GUI Application development using graphics,py

advertisement
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 "Python Programming: An Introduction to Computer Science"
(Franklin, Beedle & 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 "red"
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
Circle(p1, radius)
# 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. "arial" "courier"
setSize(size)
# set font size (5-36 are legal)
setStyle(style)
# set font style ex. "bold" "italic"
Design an interactive program to draw Basic shapes using graphics library identified in Lab
Exercise2
from graphics import *
win = GraphWin("My GUI Window", 500, 500)
win.setBackground("pink")
#Point
pt = Point(50, 50) # set the coordinates of point
pt.setOutline("blue") #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("black")
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("purple")# 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("My GUI Window", 500, 500)
win.setBackground("light blue")
Hut
#Rectangles
rect1 = Rectangle(Point(20,110),Point(120,300))
rect1.setOutline("black")
rect1.setFill("yellow")
rect1.setWidth(3)
rect1.draw(win)
rect2 = Rectangle(Point(120,110),Point(420,300))
rect2.setOutline("black")
rect2.setFill(color_rgb(255,100,50))
rect2.setWidth(3)
rect2.draw(win)
rect3 = Rectangle(Point(110,200),Point(30,300))
rect3.setOutline("black")
rect3.setFill("brown")
rect3.setWidth(3)
rect3.draw(win)
#Polygon
poly1 = Polygon(Point(70,10),Point(120,110),Point(20,110))
poly1.setFill("blue")
poly1.draw(win)
poly2 = Polygon(Point(70,10),Point(120,110),Point(420,110), Point(370,10))
poly2.setFill("purple")
poly2.draw(win)
Fish
#fish body
ov = Oval(Point(100, 370),Point(300,450))
ov.setFill("orange")
ov.draw(win)
#fish eye
circ = Circle(Point(150,390), 5)
circ.setFill("purple")
circ.draw(win)
#fish tail
poly3 = Polygon(Point(300,410),Point(350,360),Point(350,460))
poly3.setFill("blue")
poly3.draw(win)
#fish mouth
poly4 = Polygon(Point(100,410),Point(120,410),Point(100,430))
poly4.setFill("light blue")
poly4.setOutline("light blue")
poly4.draw(win)
#fins
poly3 = Polygon(Point(180,370),Point(230,340),Point(230,370))
poly3.setFill("blue")
poly3.draw(win)
poly3 = Polygon(Point(180,450),Point(230,480),Point(230,450))
poly3.setFill("blue")
poly3.draw(win)
Scenery
from graphics import *
win = GraphWin("My GUI Window", 500, 500)
win.setBackground("brown")
#grass
rect = Rectangle(Point(20,400),Point(480,480))
rect.setOutline("green")
rect.setFill("green")
rect.draw(win)
rect = Rectangle(Point(20,20),Point(480,400))
rect.setOutline("light blue")
rect.setFill("light blue")
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("yellow")
circ.draw(win)
UseCase Diagram of any one software application
from graphics import *
win = GraphWin("Use case diagram of Library management system", 500, 500)
win.setBackground("black")
#Boundary of system
rect = Rectangle(Point(150,50),Point(350,400))
rect.setFill("white")
rect.setWidth(3)
rect.draw(win)
#Use Cases
ov = Oval(Point(200, 100),Point(300,150))
ov.setFill("grey")
ov.draw(win)
ov = Oval(Point(200, 200),Point(300,250))
ov.setFill("grey")
ov.draw(win)
ov = Oval(Point(200, 300),Point(300,350))
ov.setFill("grey")
ov.draw(win)
#Actors
circ = Circle(Point(50,200), 20)
circ.setFill("white")
circ.draw(win)
circ = Circle(Point(450,200), 20)
circ.setFill("white")
circ.draw(win)
ln = Line(Point(50,200),Point(50,270))
ln.setOutline("white")
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(450,200),Point(450,270))
ln.setOutline("white")
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(30,240),Point(70,240))
ln.setOutline("white")
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(430,240),Point(470,240))
ln.setOutline("white")
ln.setWidth(5)
ln.draw(win)
#Relationships
ln = Line(Point(70,200),Point(220,140))
ln.setOutline("grey")
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(70,200),Point(210,320))
ln.setOutline("grey")
ln.setWidth(5)
ln.draw(win)
ln = Line(Point(450,220),Point(300,220))
ln.setOutline("grey")
ln.setWidth(5)
ln.draw(win)
#Text
txt=Text(Point(250,120),"Borrow book")
txt.setTextColor("white")
txt.draw(win)
txt=Text(Point(250,220),"Issue book")
txt.setTextColor("white")
txt.draw(win)
txt=Text(Point(250,330),"Return book")
txt.setTextColor("white")
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("My GUI Window", 500, 500)
#Water
rect = Rectangle(Point(20,300),Point(480,480))
rect.setOutline("blue")
rect.setFill("blue")
rect.draw(win)
#Sky
rect = Rectangle(Point(20,20),Point(480,300))
rect.setOutline("light blue")
rect.setFill("light blue")
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("yellow")
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("brown")
boat.draw(win)
#fish
#fish body
body = Oval(Point(120, 400),Point(250,450))
body.setFill("orange")
body.draw(win)
#fish eye
eye = Circle(Point(140,420), 5)
eye.setFill("purple")
eye.draw(win)
#fish tail
tail = Polygon(Point(250,422.5),Point(300,407.5),Point(300,437.5))
tail.setFill("red")
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("My GUI Window", 500, 500)
win.setBackground("black")
#Sun in the middle of Solar system
circ = Circle(Point(235,235), 25)
circ.setFill("yellow")
circ.draw(win)
#Orbits of planets
#orbit of mercury
ov = Oval(Point(170, 200),Point(300,270))
ov.setOutline("white")
ov.draw(win)
#orbit of venus
ov = Oval(Point(140, 190),Point(330,280))
ov.setOutline("white")
ov.draw(win)
#orbit of earth
ov = Oval(Point(110, 170),Point(365,295))
ov.setOutline("white")
ov.draw(win)
#orbit of mars
ov = Oval(Point(90, 150),Point(395,315))
ov.setOutline("white")
ov.draw(win)
#orbit of jupiter
ov = Oval(Point(70, 130),Point(420,325))
ov.setOutline("white")
ov.draw(win)
#orbit of saturn
ov = Oval(Point(50, 110),Point(435,340))
ov.setOutline("white")
ov.draw(win)
#orbit of uranus
ov = Oval(Point(35, 90),Point(460,360))
ov.setOutline("white")
ov.draw(win)
#orbit of neptune
ov = Oval(Point(20, 70),Point(480,380))
ov.setOutline("white")
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("orange")
circ.draw(win)
#Earth
circ = Circle(Point(140,190), 9)
circ.setFill("light green")
circ.draw(win)
#Mars
circ = Circle(Point(290,155), 7)
circ.setFill("red")
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)
Download