Python 2D Graphics
Python 2D Graphics
(0,10) (10,10)
400 pixels
(0,0) (10,0)
400 pixels
SIMPLE DRAWING
(0,10) (10,10)
(0,0) (10,0)
400 pixels
SIMPLE GRAPHICS PROGRAM
#
# graphics1.py - Simple graphics program.
#
def Main():
# Draw mouth
rect.setFill("blue");
rect.draw(win)
# Draw line
line.draw(win)
# Draw message
message.draw(win)
win.getMouse()
win.close()
Main()
SIMPLE GRAPHICS PROGRAMMING
• The simplest object is the Point. Like points in
geometry, point locations are represented with a
coordinate system (x, y), where x is the horizontal
location of the point and y is the vertical location.
• The origin (0,0) in a graphics window is the upper left
corner.
• X values increase from left to right, y values from top
to bottom.
• Lower right corner is (199, 199)
def Main():
p = Point(50, 50)
p.draw(win)
p = Point(350, 350)
p.draw(win)
win.getMouse()
# Close window
win.close()
Main()
• We can use the accessors like getX and getY or other methods on the
point returned.
def main():
win = GraphWin("Draw a Triangle")
win.setCoords(0.0, 0.0, 10.0, 10.0)
message = Text(Point(5, 0.5), "Click on three points")
message.draw(win)
triangle = Polygon(p1,p2,p3)
triangle.setFill("peachpuff")
triangle.setOutline("cyan")
triangle.draw(win)
win.getMouse()
main()
# graphical interface.
def main():
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# convert input
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32
main()