Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

Python Programming: An Introduction to Computer Science
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.
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 13: Object-Oriented Programming
Chapter 4 Objects and Graphics
Python: Graphics
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
Chapter 3 Working with Symbols and Interactivity.
Invitation to Computer Science 5th Edition
An Object-Oriented Approach to Programming Logic and Design
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.
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Working with Symbols and Interactivity
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.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Introduction to Computer Science – Chapter 8 CSc 2010 Spring 2011 Marco Valero.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Tkinter Canvas.
Some Graphics CS303E: Elements of Computers and Programming.
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.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
Creating visual interfaces in python
Computer Programming Modeling a Passive Solar Home.
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)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Programming Logic and Design Seventh Edition Chapter 12 Event-Driven GUI Programming, Multithreading, and Animation.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
Catapult Python Programming Session 4
Python Programming: An Introduction to Computer Science
Topics Graphical User Interfaces Using the tkinter Module
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python: Simple Graphics and Event-driven Programming
Graphics Part I Taken from notes by Dr. Neil Moore
Chapter 5 Working with Images
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
Working with Symbols and Interactivity
Simple Graphics Package
Topics Graphical User Interfaces Using the tkinter Module
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
Presentation transcript:

Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics

Python Programming, 2/e2 Objectives To understand the concept of objects and how they can be used to simplify programs To be familiar with some objects available in Zelle's graphics library To be able to create objects in programs and call appropriate methods to perform graphical computations

Python Programming, 2/e3 Overview Each data type can represent a certain set of values, and each had a set of associated operations The traditional programming view is that data is passive – it ’ s manipulated and combined with active operations

Python Programming, 2/e4 Overview Modern computer programs are built using an object-oriented approach Most applications you ’ re familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus There ’ s a graphics library (graphics.py) written specifically to go with this book. It ’ s based on Tkinter

Python Programming, 2/e5 The Object of Objects Basic idea – view a complex system as the interaction of simpler objects. An object is a sort of active data type that combines data and operations Objects know stuff (contain data) and they can do stuff (have operations) Objects interact by sending each other messages

Python Programming, 2/e6 The Object of Objects Suppose we want to develop a data processing system for a college or university. We must keep records on students who attend the school. Each student will be represented as an object.

Python Programming, 2/e7 The Object of Objects The student object would contain data like: Name ID number Courses taken Campus Address Home Address GPA Etc.

Python Programming, 2/e8 The Object of Objects The student object should also respond to requests. We may want to send out a campus-wide mailing, so we ’ d need a campus address for each student. We could send the printCampusAddress message to each student object. When the student object receives the message, it prints its own address.

Python Programming, 2/e9 Object of Objects Objects may refer to other objects. Each course might be represented by an object: Instructor Student roster Prerequisite courses When and where the class meets

Python Programming, 2/e10 Sample Operations ista130 = Course("CESL 102") ista130.addStudent('Jessie') ista130.addStudent('Casey') ista130.removeStudent('Jessie') ista130.changeRoom("CESL103") print(ista130.numberOfStudents() ) # Guess the Output

str objects are immutable, cannot change them can be referenced with a variable aStrObject = str('a string object') have data (not seen) a sequence of characters an integer as current number of characters locale like U.S., Germany, or China,... have operations (methods) upped = aStrObject.upper() 11

Other str operations Python Programming, 2/e12 s = str('Jessie Casey') # a str object s.count('e') # Value? s.index('ssi') # Value? s.index('Not') # Value? s.endswith('sey') # Value? s.split(' ') # Value?

Python Programming, 2/e13 Simple Graphics Programming This chapter uses the graphics.py library supplied with the supplemental materials Two location choices In Python ’ s Lib directory with other libraries In the same folder as your graphics program (what we'll do) like copying Rick's test modules

Python Programming, 2/e14 Simple Graphics Programming Import all functions with '*' and avoid using graphics. in graphics. graphWin() from graphics import * A graphics window is a place on the screen where the graphics will appear win = GraphWin() What type of object is win ?

Python Programming, 2/e15 Simple Graphics Programming Windows can be closed/destroyed by issuing the command win.close()

Python Programming, 2/e16 Simple Graphics Programming A graphics window is a collection of points called pixels (picture elements). The default GraphWin is 200 pixels tall by 200 pixels wide (40,000 pixels total). 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 routines to draw geometric shapes.

Python Programming, 2/e17 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)

Python Programming, 2/e18 Point and Circle objects from graphics import * win = GraphWin() p = Point(50, 60) p.draw(win) p.getX() # Value? ____ p.getY() # Value? ____ circ = Circle(p, 20) circ.draw(win) # What is the size of the # radius in pixels? _____

Python Programming, 2/e19 Using Graphical Objects Computation is preformed by asking an object to carry out one of its operations In the previous examples we manipulated str, GraphWin, and Point These are examples of classes

Python Programming, 2/e20 Using Graphical Objects The class describes the properties of the instance: the data and operations We will write our own classes in chapter 10 Each object is an instance of some class, If we say that Augie is a dog, then Augie is a specific individual in the larger class of all dogs. Augie is an instance of the dog class.

Python Programming, 2/e21 Using Graphical Objects To create a new instance of a class, we use a special operation called a constructor (,, …) is the type of object s1 = str('Leslie') p = Point(50, 60) c = Circle(p, 20) The arguments become the data remembered by the object To create a new instance of a class, we use a

Python Programming, 2/e22 Using Graphical Objects Only the most relevant instance variables are shown (others include the color, window they belong to, etc.)

Python Programming, 2/e23 Using Graphical Objects To perform an operation on an object, we send the object a message. The set of messages an object responds to are called the methods of the object. Methods are functions that live inside the object. Methods are invoked using dot-notation:. (,, …) s.rjust(8,'*') # s is str object p.draw(win) # p is a Point object fromLeft = p.getX() c.setFill('yellow') # c is a Circle object

24 Circle remembers its Point circ = Circle(Point(100, 100), 30) win = GraphWin() circ.draw(win)

Python Programming, 2/e25 Rectangle Objects upperLeft = Point(50, 50) lowerRight = Point(150, 80) rect = Rectangle(upperLeft, lowerRight) rect.setFill('yellow') rect.setOutline('red') rect.draw(win) # Is rect's height > width ______ # What color is rect's border? ______ # How many pixels are yellow if the border # is 1 pixel wide? ______

Python Programming, 2/e26 The GraphWin object