NextClass Assignment 3
This objective of this assignment is to prepare you for the first lab and its week-long programming assignment. In that lab you will use the Graphics library that we have installed, graphics.py, and will build a picture of your own creation with the basic tools provided by the graphics library. These tools include the ability to create and draw (and fill with colors of your choosing): Points, Lines, Circles, Rectangles, Ovals, Polygons, and Text, along with facilities for interacting with the user by getting the coordinates of mouse clicks and putting up dialog boxes asking for user input.
The key to successfully using the library, at its core, comes down to the ability to invoke (or call) functions, which are the Python programming building blocks being provided by the library. You don't need to know how the library does its job of creating and updating a graphics window, but you just need to learn how to make the Python function call statement that specifies what you want done.
Exercise 1
Enter the following Python program, naming it nc3-1.py.# Author: # Date: # Objective: Bare bones example of creating a graphics window, # without finishing too quickly.
from graphics import *
win = GraphWin("First Window", 200, 100)
win.getMouse() win.close()
Quick discussion of each of the lines:
The 'import' line is the one that brings all the functions of the graphics library into the current program.
The next line is an assignment statement. So win is just the name of a variable that we wish to use to remember the value returned by the GraphWin() function call (or invocation). When a particular function generates and yields a value, we typically want the ability to use that value somewhere downstream in our program, so these functions often appear on the "right hand side" of an assignment statement. The data type returned by the GraphWin() function is known generically as an object data type. All function invocations must include the function name followed by a begin and end paren. In between the parenthesis are all of the parameters that are used to specify the arguments to the function call. We use these to govern how the function does its job, letting it know additional information.
The final two lines are also funtcion calls. The function names are getMouse and close. In this case, both functions are 'part of' the win object, which is why we have the 'win.' syntax before the function name. Neither of these functions return a value as their result (or we don't care about their value), which is why they appear as simple statements, and are not part of an assignment. Neither have any parameters, and so there is nothing between the begin and the end paren. Note that, when we run the program, the getMouse() function call causes us to wait in our execution until the user clicks the mouse within the boundaries of the window, at which point execution continues and the window closes due to the close() function call.
Before moving on to the next exercise, try changing the text string and the two integer numbers that are the arguments to the GraphWin() function in various ways and combinations. Also note what happens if you were to put the integers first and then the string. Try adding the comment character to the beginning of the getMouse() function call. What happens?
Exercise 2
In this next exercise, we add code in between the Exercise 1 'bare bones' to create and draw some objects. Enter the following Python program, naming it nc3-2.py.# Author: # Date: # Objective: Example of creating a graphics window, # and adding some basic circle objects to # the window.
from graphics import *
win = GraphWin("Second Window", 200, 100)
center1 = Point(50, 75) circle1 = Circle(center1, 40) circle1.setFill('red') circle1.draw(win)
circle2 = Circle(center1, 20) circle2.setFill('blue') circle2.draw(win)
circle3 = Circle(Point(100,40), 30) circle3.draw(win) circle3.setFill('green')
win.getMouse() win.close()