CS177 Python Programming: Objects and Graphics
CS177 Python Programming: Objects and Graphics
CS177 Python Programming: Objects and Graphics
Chapter 4
Objects and Graphics
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 line
line = Line(Point(1, 8), Point(9, 8))
line.draw(win)
# Draw message
message = Text(Point(5, 0.5), "Click anywhere to quit")
message.draw(win)
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 right to left, y values from
top to bottom.
• Lower right corner is (199, 199)
# Close window
win.close()
Main()
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)
main()
def main():
win = GraphWin("Celsius Converter", 300, 200)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# convert input
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32
main()