Python: Graphics

Slides:



Advertisements
Similar presentations
Python Programming: An Introduction to Computer Science
Advertisements

Python Programming: An Introduction to Computer Science
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Flowchart Start Input weight and height
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Chapter 4 Objects and Graphics
Animation CSC 161: The Art of Programming Prof. Henry Kautz 10/14/2009.
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
Chapter 3 Working with Symbols and Interactivity.
Chapter 5 Graphics.  We’ve been doing command line programming, but now it’s time to try GUI programming—programming in a graphical user interface, using.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Working with Symbols and Interactivity
Visual Basic 2005 CHAPTER 2 Program and Graphical User Interface Design.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Laboratory Exercise # 9 – Inserting Graphics to Documents Office Productivity Tools 1 Laboratory Exercise # 9 Inserting Graphics to Documents Objectives:
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
Some Graphics CS303E: Elements of Computers and Programming.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Python Programming, 1/e1 Programming Thinking and Method (5a) Zhao Hai 赵海 Department of Computer Science and Engineering Shanghai Jiao Tong University.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
Creating visual interfaces in python
Animation II: Revenge of the Clones CSC 161: The Art of Programming Prof. Henry Kautz 10/16/2009.
Interaction with Graphics Also displaying GIFs. Text Output on the graphics window There is a class called Text which will put the message on the graphics.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
Adobe Photoshop T.Ahlam Algharasi. Adobe Photoshop Adobe Photoshop is a seriously powerful photo and image edit ( treating and manipulation, compositing,
10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
5. COMPUTING WITH GRAPHICS OBJECTS Dennis Y. W. Liu and Rocky K. C. Chang October 8, 2015 (Adapted from John Zelle’s slides)
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
CIS 205—Web Design & Development Flash Chapter 3 Working with Symbols and Interactivity.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Killer cars.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
GUI in Python Ismail Abumuhfouz.
MOM! Phineas and Ferb are … Aims:
Python Programming: An Introduction to Computer Science
PYGAME.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Chapter 21 – Graphics/Tk Outline 21.1 Introduction 21.2 GD Module: Creating Simple Shapes 21.3 GD Module: Image Manipulation 21.4 Chart Module 21.5 Introduction.
Python Programming: An Introduction to Computer Science
An Introduction to Computers and Visual Basic
Python: Simple Graphics and Event-driven Programming
An Introduction to Computers and Visual Basic
Graphics Part I Taken from notes by Dr. Neil Moore
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Graphics Part I Taken from notes by Dr. Neil Moore
An Introduction to Computers and Visual Basic
Topics Graphical User Interfaces Using the tkinter Module
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey
Presentation transcript:

Python: Graphics

Simple Graphics Module Our book uses a file named “graphics.py”  This is not a standard module  Must download it from the web  Must make sure it is:  In the same directory as any code that imports it OR  In a system path directory The package has classes and methods for  Creating windows and graphical components  Drawing points, lines, rectangles, circles

Simple Graphics Module A GraphWin object represents a window on the screen where graphical images may be drawn. A program may define any number of GraphWins.  GraphWin(title, width, height): Constructs a new graphics window for drawing on the screen. The parameters are optional, the default title is ``Graphics Window,'' and the default size is 200 x 200.  plot(x, y, color): Draws the pixel at in the window. Color is optional, black is the default.  plotPixel(x, y, Color): Draws the pixel at the ``raw'' position ignoring any coordinate transformations set up by setCoords.  setBackground(color): Sets the window background to the given color. The initial background is gray.  close(): Closes the on-screen window.  getMouse(): Pauses for the user to click a mouse in the window and returns where the mouse was clicked as a Point object.  checkMouse(): Similar to getMouse, but does not pause for a user click. Returns the latest point where the mouse was clicked or None if the window as not been clicked since the previous call to checkMouse or getMouse. This is particularly useful for controlling simple animation loops.  setCoords(xll, yll, xur, yur): Sets the coordinate system of the window. The lower-left corner is and the upper-right corner is. All subsequent drawing will be done with respect to the altered coordinate system (except for plotPixel).

4 Simple Graphics Programming Since this is a library, we need to import the graphics command  >>> import graphics A graphics window is a placed on the screen where the graphics will appear.  >>> win = graphics.GraphWin() May want to close the window  >>> win.close()

5 Simple Graphics Programming It ’ s tedious to use the long notation to access the graphics library routines. Can import functions from a module by  from graphics import * The “from” statement allows you to load specific functions from a library module. “*” will load all the functions, or you can list specific ones. Doing the import this way eliminates the need to preface graphics commands with graphics.  >>> from graphics import *  >>> win = GraphWin()

6 Simple Graphics Programming A graphics window is a collection of points called pixels. The default GraphWin is 200 pixels tall by 200 pixels wide. One way to get pictures into the window is one pixel at a time, which would be tedious. The graphics routine has a number of predefined objects and routines to draw geometric shapes. 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)

Simple Graphics Programming 7 >>> p = Point(50, 60) >>> p.getX() 50 >>> p.getY() 60 >>> win = GraphWin() >>> p.draw(win) >>> p2 = Point(140, 100) >>> p2.draw(win)

Simple Graphics Programming 8 >>> ### Open a graphics window >>> win = GraphWin('Shapes') >>> ### Draw a red circle centered at point (100, 100) with radius 30 >>> center = Point(100, 100) >>> circ = Circle(center, 30) >>> circ.setFill('red') >>> circ.draw(win) >>> ### Put a textual label in the center of the circle >>> label = Text(center, "Red Circle") >>> label.draw(win) >>> ### Draw a square using a Rectangle object >>> rect = Rectangle(Point(30, 30), Point(70, 70)) >>> rect.draw(win) >>> ### Draw a line segment using a Line object >>> line = Line(Point(20, 30), Point(180, 165)) >>> line.draw(win) >>> ### Draw an oval using the Oval object >>> oval = Oval(Point(20, 150), Point(180, 199)) >>> oval.draw(win)

9 Using Graphical Objects Accessor methods: methods that return information about an object without changing the state of the object.  p.getX() and p.getY() returns the x and y values of the point. Setter or mutator methods: methods that change the state of the object by changing the objects attributes.  move(dx, dy) moves the object dx units in the x direction and dy in the y direction. Move erases the old image and draws it in its new position.

10 Using Graphical Objects It ’ s possible for two different variables to refer to the same object – changes made to the object through one variable will be visible to the other. >>> leftEye = Circle(Point(80,50), 5) >>> leftEye.setFill('yellow') >>> leftEye.setOutline('red') >>> rightEye = leftEye >>> rightEye.move(20,0) The idea is to create the left eye and copy that to the right eye which gets moved 20 units.

11 Using Graphical Objects There are two ways to get around this. We could make two separate circles, one for each eye: >>> leftEye = Circle(Point(80, 50), 5) >>> leftEye.setFill('yellow') >>> leftEye.setOutline('red') >>> rightEye = Circle(Point(100, 50), 5) >>> rightEye.setFill('yellow') >>> rightEye.setOutline('red')

12 Using Graphical Objects The graphics library has a better solution. Graphical objects have a clone method that will make a copy of the object! >>> # Correct way to create two circles, using clone >>> leftEye = Circle(Point(80, 50), 5) >>> leftEye.setFill('yellow') >>> leftEye.setOutline('red') >>> rightEye = leftEye.clone() >>> rightEye.move(20, 0)

Graphing Future Value/Choosing Coordinates 13

Graphing Future Value/Choosing Coordinates 14

15 Interactive Graphics In a GUI environment, users typically interact with their applications by clicking on buttons, choosing items from menus, and typing information into on- screen text boxes. Event-driven programming draws interface elements (widgets) on the screen and then waits for the user to do something.

Python Programming, 2/e 16 Interactive Graphics An event is generated whenever a user moves the mouse, clicks the mouse, or types a key on the keyboard. An event is an object that encapsulates information about what just happened! The event object is sent to the appropriate part of the program to be processed, for example, a button event.

Python Programming, 2/e 17 Getting Mouse Clicks We can get graphical information from the user via the getMouse method of the GraphWin class. When getMouse is invoked on a GraphWin, the program pauses and waits for the user to click the mouse somewhere in the window. The spot where the user clicked is returned as a Point.

Python Programming, 2/e 18 Getting Mouse Clicks The following code reports the coordinates of a mouse click: from graphics import * win = GraphWin("Click Me!") p = win.getMouse() print("You clicked", p.getX(), p.getY()) We can use the accessors like getX and getY or other methods on the point returned.

Python Programming, 2/e 19 Getting Mouse Clicks # triangle.pyw # Interactive graphics program to draw a triangle from graphics import * 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) # Get and draw three vertices of triangle p1 = win.getMouse() p1.draw(win) p2 = win.getMouse() p2.draw(win) p3 = win.getMouse() p3.draw(win)

Python Programming, 2/e 20 Getting Mouse Clicks # Use Polygon object to draw the triangle triangle = Polygon(p1,p2,p3) triangle.setFill("peachpuff") triangle.setOutline("cyan") triangle.draw(win) # Wait for another click to exit message.setText("Click anywhere to quit.") win.getMouse() main()

Python Programming, 2/e 21 Getting Mouse Clicks

Python Programming, 2/e 22 Getting Mouse Clicks Notes:  If you are programming in a windows environment, using the.pyw extension on your file will cause the Python shell window to not display when you double-click the program icon.  There is no triangle class. Rather, we use the general polygon class, which takes any number of points and connects them into a closed shape.

Python Programming, 2/e 23 Getting Mouse Clicks  Once you have three points, creating a triangle polygon is easy: triangle = Polygon(p1, p2, p3)  A single text object is created and drawn near the beginning of the program. message = Text(Point(5,0.5), "Click on three points") message.draw(win)  To change the prompt, just change the text to be displayed. message.setText("Click anywhere to quit.")

Python Programming, 2/e 24 Handling Textual Input The triangle program ’ s input was done completely through mouse clicks. There ’ s also an Entry object that can get keyboard input. The Entry object draws a box on the screen that can contain text. It understands setText and getText, with one difference that the input can be edited.

Python Programming, 2/e 25 Handling Textual Input

Python Programming, 2/e 26 Handling Textual Input # convert_gui.pyw # Program to convert Celsius to Fahrenheit using a simple # graphical interface. from graphics import * def main(): win = GraphWin("Celsius Converter", 300, 200) win.setCoords(0.0, 0.0, 3.0, 4.0) # Draw the interface Text(Point(1,3), " Celsius Temperature:").draw(win) Text(Point(1,1), "Fahrenheit Temperature:").draw(win) input = Entry(Point(2,3), 5) input.setText("0.0") input.draw(win) output = Text(Point(2,1),"") output.draw(win) button = Text(Point(1.5,2.0),"Convert It") button.draw(win) Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)

Python Programming, 2/e 27 Handling Textual Input # wait for a mouse click win.getMouse() # convert input celsius = eval(input.getText()) fahrenheit = 9.0/5.0 * celsius + 32 # display output and change button output.setText(fahrenheit) button.setText("Quit") # wait for click and then quit win.getMouse() win.close() main()

Python Programming, 2/e 28 Handling Textual Input

Python Programming, 2/e 29 Handling Textual Input When run, this program produces a window with an entry box for typing in the Celsius temperature and a button to “ do ” the conversion.  The button is for show only! We are just waiting for a mouse click anywhere in the window.

Python Programming, 2/e 30 Handling Textual Input Initially, the input entry box is set to contain “ 0.0 ”. The user can delete this value and type in another value. The program pauses until the user clicks the mouse – we don ’ t care where so we don ’ t store the point!

Python Programming, 2/e 31 Handling Textual Input The input is processed in three steps:  The value entered is converted into a number with eval.  This number is converted to degrees Fahrenheit.  This number is then converted to a string and formatted for display in the output text area.