We have introduced most of the important concepts and methods. Package with the form of all function calls is at A systematic reference to Zelle’s graphics Thus far various parts of Zelle’s graphics package have been Via the getMouse method, or via the input keyboardĢ.4.5. Specified at a time (either a mouse click in the graphics window If the programmer wants user input, only one type can be Package, the complexities of the event driven model are pretty well Programming has a considerable learning curve. Sources including mouse clicks and key presses. The program can be interrupted by input from many Modern graphics environments areĮvent driven. This lack of robustness is tied to the simplification designed into In this case the closeīutton on the title bar is important: it is the easiest method to The graphics window may be left orphaned. Window and then terminates abnormally due to some other error, On the other hand, if your program creates a graphics Graphics window that way, you are likely to get a Python error Operating system close button on the title bar. If a graphics window is closed by clicking on the standard One limitation of the graphics.py module is that it is not robust This optional section only looks forward to more elaborate graphics setText ( 'Click anywhere to quit' ) # change text message win. setWidth ( 4 ) # width of boundary line triangle. draw ( win ) vertices = # Use Polygon object to draw the triangle triangle = Polygon ( vertices ) triangle. draw ( win ) # Get and draw three vertices of triangle p1 = win. getWidth () / 2, 30 ), 'Click on three points' ) message. setBackground ( 'yellow' ) message = Text ( Point ( win. ''' from graphics import * def main (): win = GraphWin ( 'Draw a Triangle', 350, 350 ) win. '''Program: triangle.py or triangle.pyw (best name for Windows) Interactive graphics program to draw a triangle, with prompts in a Text object and feedback via mouse clicks. Window and click with the mouse as requested.Īfter you have run the program, you can examine the program in Idle
While running the program, follow the prompts in the graphics Or on a Mac, you can run it using the Python Launcher, rather than Idle. You may be able to double click on the icon for triangle.py to run it. Return to the directory window for the Python examples. The Points where the user clicked the mouse.
#Endless canvas drawing program code#
Rather than the code specifying literal coordinates for all In addition it explicitly interacts with the user. The next example, triangle.py, illustrates similar starting andĮnding code. Location of the center of each balloon, so a loop over a list of Note that the steps for theĬreation of all three balloons are identical, except for the Is to make the Shell window be the current window and select from the menuĪnother simple drawing example is balloons.py. Because such a sequence is so common, I haveĪdded another method for Zelle’s GraphWin objects, promptClose,Īnd the program bombs out while there is a GraphWin on the screen,Ī dead GraphWin lingers. We will generally want to prompt the user to finally close the Terminated after the last line finishes executing, that is not trueįor programs that create new windows: The graphics window must beĮxplicitly closed. While our earlier text-based Python programs have automatically
This mouse click will be used.) As you have seen before, Position of the mouse click is not important. Using win.getWidth()/2 means the horizontal position is set up to beĬentered, half way across the window’s width.Īfter the first two lines draw the prompting text, the line The new addition to the Text parameters here is win.getWidth() getWidth () / 2, 20 ), 'Click anywhere to quit.' ) message. Modern screen usually have more than 1000 A pixel is the smallest little square that can byĭisplayed on your screen. Our graphics window object.) A small window, 200 by 200 pixels isĬreated. The assignment statement remembers the window object as Graphics package that automatically displays a window when it isĬreated. A GraphWin is a type of object from Zelle’s To the top, and preferably drag it around to make it visible beside There should be a new window labeled “Graphics Window”, created Look around on your screen, and possibly underneath other windows: Zelle’s module accessible, as if they were already defined like The first line above makes all the types of object of For the Python interpreter to find Zelle’s module, it Zelle’s graphics are not a part of the standard Pythonĭistribution.