Graphics programming in python pdf




















Most of the examples in this section refer to a Turtle instance called turtle. Move the turtle forward by the specified distance , in the direction the turtle is headed. Move the turtle backward by distance , opposite to the direction the turtle is headed. Turn turtle right by angle units. Units are by default degrees, but can be set via the degrees and radians functions.

Angle orientation depends on the turtle mode, see mode. Turn turtle left by angle units. If y is None , x must be a pair of coordinates or a Vec2D e. Move turtle to an absolute position.

If the pen is down, draw line. Here are some common directions in degrees:. Move turtle to the origin — coordinates 0,0 — and set its heading to its start-orientation which depends on the mode, see mode. Draw a circle with given radius. The center is radius units left of the turtle; extent — an angle — determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position.

Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent. As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use. If not given, it will be calculated automatically.

May be used to draw regular polygons. Draw a circular dot with diameter size , using color. Stamp a copy of the turtle shape onto the canvas at the current turtle position. Undo repeatedly the last turtle action s. Number of available undo actions is determined by the size of the undobuffer.

If no argument is given, return current speed. If input is a number greater than 10 or smaller than 0. Speedstrings are mapped to speedvalues as follows:. Return the angle between the line from turtle position to position specified by x,y , the vector or the other turtle. Return the distance from the turtle to x,y , the given vector, or the given other turtle, in turtle step units.

Set angle measurement units, i. Default value is degrees. Set the angle measurement units to radians. Set the line thickness to width or return it. If no argument is given, the current pensize is returned. This dictionary can be used as argument for a subsequent call to pen to restore the former pen-state.

Moreover one or more of these attributes can be provided as keyword-arguments. This can be used to set several pen attributes in one statement. Return the current pencolor as color specification string or as a tuple see example. Set pencolor to colorstring , which is a Tk color specification string, such as "red" , "yellow" , or " 33cc8c".

Set pencolor to the RGB color represented by the tuple of r , g , and b. Each of r , g , and b must be in the range Set pencolor to the RGB color represented by r , g , and b. Return the current fillcolor as color specification string, possibly in tuple format see example.

Set fillcolor to colorstring , which is a Tk color specification string, such as "red" , "yellow" , or " 33cc8c". Set fillcolor to the RGB color represented by the tuple of r , g , and b. Set fillcolor to the RGB color represented by r , g , and b. Return the current pencolor and the current fillcolor as a pair of color specification strings or tuples as returned by pencolor and fillcolor. Inputs as in pencolor , set both, fillcolor and pencolor, to the given value.

Equivalent to pencolor colorstring1 and fillcolor colorstring2 and analogously if the other input format is used. If turtleshape is a polygon, outline and interior of that polygon is drawn with the newly set colors.

See also: Screen method colormode. Return fillstate True if filling, False else. Whether or not overlap regions for self-intersecting polygons or multiple shapes are filled depends on the operating system graphics, type of overlap, and number of overlaps.

For example, the Turtle star above may be either all yellow or have some white regions. Do not move turtle. State and position of the turtle as well as drawings of other turtles are not affected.

If move is true, the pen is moved to the bottom-right corner of the text. By default, move is False. Make the turtle invisible. Set turtle shape to shape with given name or, if name is not given, return name of current shape. If rmode is not given, return current resizemode. Different resizemodes have the following effects:.

Set or return the current shearfactor. After creating the GraphWin Object you can use the graphics functions from graphics. Output of the graphics functions are displayed with draw method. It is called with the shape object to be displayed and passing GraphWin object as parameter. This is the last part of any Python Graphics program. The easiest way to ensure this is to start in the desired folder, as discussed in Starting Idle for Editing. To work on the most systems, this version of graphics.

There is an issue with the use of multiple threads of execution. You will just be a user of the graphics. It uses all sorts of features of Python that are way beyond these tutorials.

There is no particular need to open graphics. Load into Idle and start running example graphIntroSteps. Each time you press return, look at the screen and read the explanation for the next line s. Bring it to the top, and preferably drag it around to make it visible beside your Shell window.

The assignment statement remembers the window object as win for future reference. This will be our standard name for our graphics window object. A small window, by pixels is created. A pixel is the smallest little square that can by displayed on your screen. Modern screen usually have more than pixels across the whole screen.

This creates a Point object and assigns it the name pt. Unlike when a GraphWin is created, nothing is immediately displayed: In theory you could have more than one GraphWin. Zelle designed the graphics module so you must tell Python into which GraphWin to draw the Point.

A Point object, like each of the graphical objects that can be drawn on a GraphWin , has a method [1] draw. Now you should see the Point if you look hard in the Graphics Window - it shows as a single, small, black pixel. Graphics windows have a Cartesian x,y coordinate system.

The dimensions are initially measured in pixels. The first coordinate is the horizontal coordinate, measured from left to right, so is about half way across the pixel wide window. The second coordinate, for the vertical direction, increases going down from the top of the window by default, not up as you are likely to expect from geometry or algebra class.

We will see later that we can reorient the coordinate system to fit our taste. Henceforth you will see a draw method call after each object is created, so there is something to see. The first line creates a Circle object with center at the previously defined pt and with radius This object is remembered with the name cir. As with all graphics objects that may be drawn within a GraphWin , it is only made visible by explicitly using its draw method.

So far, everything has been drawn in the default color black. Graphics objects like a Circle have methods to change their colors. Basic color name strings are recognized. You can choose the color for the circle outline as well as filling in the inside. Note the method names.

They can be used with other kinds of Graphics objects, too. We delay a discussion of fancier colors until Color Names and Custom Colors. A Line object is constructed with two Points as parameters. In this case we use the previously named Point, pt , and specify another Point directly. Technically the Line object is a segment between the the two points. In Python , is a tuple , not a Point.

To make a Point , you must use the full constructor: Point , Point s, not tuple s, must be used in the constructors for all graphics objects. In this simple system, a Rectangle is restricted to have horizontal and vertical sides. A Polygon , introduced in the next section, is used for all more general straight-sided shapes.

You can move objects around in a GraphWin. Shortly this will be handy for animation. The parameters to the move method are the amount to shift the x and y coordinates. See if you can guess the result before you press return :. Take your last look at the Graphics Window, and make sure that all the steps make sense. Then destroy the window win with the GraphWin method close.

The example program graphIntro. At the end of graphIntro. You can load graphIntro. Of course you will not see their effect until you run the whole program! Unfortunately the graphics do not work when entered directly into the Shell. In graphIntro. Instead consider a very simple example program, face. The only interaction is to click the mouse to close the graphics window.

In Windows, have a folder window open to the Python examples folder containing face. If that does not work on your system, you can always run from inside Idle. After you have checked out the picture, click with the mouse inside the picture, as requested, to terminate the program.

After you have run the program, you can examine the program in Idle or look below. The whole program is shown first; smaller pieces of it are discussed later:. Immediately after the documentation string, always have the import line in your graphics program, to allow easy access to the graphics.

The first line shows the more general parameters for constructing a new GraphWin, a window title plus width and height in pixels.

The second line shows how to turn the coordinate system right-side-up, so the y coordinate increases up the screen, using the yUp method. Thereafter, all coordinates are given in the new coordinate system. All the lines of code up to this point in the program are my standard graphics program starting lines other than the specific values for the title and dimensions.

You will likely start your programs with similar code. The lines above create two circles, in each case specifying the centers directly. They are filled in and made visible. Also note, that because the earlier win. If the code was switched to put the head part second, the eye would become hidden. The most recent thing drawn is on top.

The code above draws and displays a line, and illustrates another method available to graphics object, setWidth , making a thicker line. The code above illustrates another kind of graphics object, an Oval or ellipse. There are several ways an oval could be specified. Zelle chose to have you specify the corners of the bounding box that is just as high and as wide as the oval.

This rectangle is only imagined, not actually drawn. If you want to see such a rectangle, create a Rectangle object with the same two Points as parameters The code above illustrates how a Text object is used to place text on the window.

The parameters to construct the Text object are the point at the center of the text, and the text string itself.

The exact coordinates for the parts were determined by a number of trial-and-error refinements to the program. An advantage of graphics is that you can see the results of your programming, and make changes if you do not like the results! In this simple system, there is not a good way to predict the dimensions of text on the screen. The final action is to have the user signal to close the window.

Just as with waiting for keyboard input from input , it is important to prompt the user before waiting for a response! In a GraphWin, that means using prompt must be made with a Text object displayed explicitly before the response is expected.

The new addition to the Text parameters here is win. There is also a win. Using win. After the first two lines draw the prompting text, the line win. In this program, the position of the mouse click is not important. In the next example the position of this mouse click will be used. As you have seen before, win.

While our earlier text-based Python programs have automatically terminated after the last line finishes executing, that is not true for programs that create new windows: The graphics window must be explicitly closed.

The win. We will generally want to prompt the user to finally close the graphics window. The modified program is in face2. You can copy the form of this program for other simple programs that just draw a picture. The size and title on the window will change, as well as the specific graphical objects, positions, and colors. Something like the last line can be used to terminate the program. If you write a program with a bug, and the program bombs out while there is a GraphWin on the screen, a dead GraphWin lingers.

Another simple drawing example is balloons. Feel free to run it and look at the code in Idle. Note that the steps for the creation of all three balloons are identical, except for the location of the center of each balloon, so a loop over a list of the centers makes sense.

The next example, triangle. In addition it explicitly interacts with the user. Rather than the code specifying literal coordinates for all graphical objects, the program remembers the Points where the user clicked the mouse. They are used as the vertices of a triangle. Return to the directory window for the Python examples.

In Windows you may be able to double click on the icon for triangle. Or on a Mac, you can run it using the Python Launcher, rather than Idle. While running the program, follow the prompts in the graphics window and click with the mouse as requested.

It assumes familiarity with vectors, matrices, geometry and trigonometry. Articulate Storyline Essentials. Beginning SharePoint Development. Beginning SharePoint Beginning PowerShell for SharePoint Please enter your comment!



0コメント

  • 1000 / 1000