靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.

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.
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Python Programming: An Introduction to Computer Science
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 4 Objects and Graphics
Python: 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.
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
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.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Working with Symbols and Interactivity
Some Graphics CS303E: Elements of Computers and 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.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
Creating visual interfaces in python
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.
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.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
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.
Fundamentals of Windows Mouse n 4 Basic Operations: –Pointing –Clicking –Double Clicking –Dragging.
Dive Into® Visual Basic 2010 Express
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Development Environment
GUI in Python Ismail Abumuhfouz.
Catapult Python Programming Session 4
Python Programming: An Introduction to Computer Science
Working in the Forms Developer Environment
Topics Graphical User Interfaces Using the tkinter Module
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
An Introduction to Computers and Visual Basic
Console and GUI Programs
Java Programming: From Problem Analysis to Program Design,
Python: Simple Graphics and Event-driven Programming
An Introduction to Computers and Visual Basic
Graphics Part I Taken from notes by Dr. Neil Moore
Fundamentals of Python: From First Programs Through Data Structures
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Tkinter GUIs Computer Science and Software Engineering
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Graphics Part I Taken from notes by Dr. Neil Moore
Working with Symbols and Interactivity
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Simple Graphics Package
An Introduction to Computers and Visual Basic
Topics Graphical User Interfaces Using the tkinter Module
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
Class 6 using the math library
Chopin’s Nocturne.
Chapter 1 Introducing Small Basic
Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey
Presentation transcript:

靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱

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

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

Objectives (cont.) To understand the fundamental concepts of computer graphics, especially the role of coordinate systems and coordinate transformations. To understand how to work with both mouse and text-based input in a graphical programming context. Python Programming, 2/e

Objectives (cont.) To be able to write simple interactive graphics programs using the graphics library. Python Programming, 2/e

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/e

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. Tkinter is the Python interpreter to Tcl/Tk. Python Programming, 2/e

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/e

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/e

The Object of Objects The student object would contain data like: Name ID number Courses taken Campus Address Home Address GPA (Grade Point Average) etc. Python Programming, 2/e

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 to each student object. When the student object receives the message, it prints its own address. Python Programming, 2/e

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/e

Object of Objects Sample Operation addStudent delStudent changeRoom etc. Python Programming, 2/e

Demo of TA as an Object Birth Year Living City Courses Taken AddrCity() CourseList() Python Programming, 2/e

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

Simple Graphics Programming Since this is a library, we need to import the graphics commands >>> import graphics A graphics window is a place on the screen where the graphics will appear. >>> win = graphics.GraphWin() This command creates a new window titled “Graphics Window.” Python Programming, 2/e

Simple Graphics Programming GraphWin is an object assigned to the variable win. We can manipulate the window object through this variable, similar to manipulating files through file variables. Windows can be closed/destroyed by issuing the command >>> win.close() Python Programming, 2/e

Simple Graphics Programming It’s tedious to use the graphics. notation to access the graphics library routines. 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. Python Programming, 2/e

Simple Graphics Programming Doing the import this way eliminates the need to preface graphics commands with graphics. >>> from graphics import * >>> win = GraphWin() Python Programming, 2/e

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/e Python Programming, 1/e

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/e

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

Simple Graphics Programming >>> ### 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) Python Programming, 2/e

Roll up your sleeves Download graphics.py from graphics import * Create a window by GraphWin() Draw some objects Point, Circle, Rectangle, Oval, Line, Text Python Programming, 2/e

Change the Color of an Object from graphics import * win = GraphWin() c = Circle(Point(100,100), 50) c.draw(win) c.setFill("white") c.setOutline("pink") Python Programming, 2/e

Using Graphical Objects Computation is preformed by asking an object to carry out one of its operations. In the previous example we manipulated GraphWin, Point, Circle, Oval, Line, Text and Rectangle. These are examples of classes. Python Programming, 2/e

Using Graphical Objects Each object is an instance of some class, and the class describes the properties of the instance. If we say that Jimmy is a dog, we are actually saying that Jimmy is a specific individual in the larger class of all dogs. Jimmy is an instance of the dog class. Python Programming, 2/e

Using Graphical Objects To create a new instance of a class, we use a special operation called a constructor. <class-name>(<param1>, <param2>, …) <class-name> is the name of the class we want to create a new instance of, e.g. Circle or Point. The parameters are required to initialize the object. For example, Point requires two numeric values. Python Programming, 2/e

Using Graphical Objects p = Point(50, 60) The constructor for the Point class requires to parameters, the x and y coordinates for the point. These values are stored as instance variables inside of the object. Python Programming, 2/e

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

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 like functions that live inside the object. Methods are invoked using dot-notation: <object>.<method-name>(<param1>, <param2>, …) Python Programming, 2/e

Using Graphical Objects p.getX() and p.getY() returns the x and y values of the point. Routines like these are referred to as accessors because they allow us to access information from the instance variables of the object. Python Programming, 2/e

Using Graphical Objects Other methods change the state of the object by changing the values of the object’s instance variables. 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. Methods that change the state of an object are called mutators. Python Programming, 2/e

Using Graphical Objects >>> circ = Circle(Point(100, 100), 30) >>> win = GraphWin() >>> circ.draw(win) The first line creates a circle with radius 30 centered at (100,100). We used the Point constructor to create a location for the center of the circle. The last line is a request to the Circle object circ to draw itself into the GraphWin object win. Python Programming, 2/e

Using Graphical Objects The draw method uses information about the center and radius of the circle from the instance variable. Python Programming, 2/e

Objects Aliasing 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. Python Programming, 2/e

Objects Aliasing The assignment rightEye = leftEye makes rightEye and leftEye refer to the same circle! The situation where two variables refer to the same object is called aliasing. Python Programming, 2/e

Objects Aliasing Python Programming, 2/e

Objects Cloning 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') Python Programming, 2/e

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 is an exact copy of the left >>> rightEye.move(20, 0) Python Programming, 2/e

Hands-On move() clone() Python Programming, 2/e

Write a Program to Graph Future Value Python Programming, 2/e

Future Value (Chapter 2, P.46) Print an introduction Input the amount of the principal (principal) Input the annual percentage rate (apr) Repeat 10 times: principal = principal * (1 + apr) Output the value of principal Python Programming, 2/e

futval.py # futval.py # A program to compute the value of an investment # carried 10 years into the future def main(): print("This program calculates the future value") print("of a 10-year investment.") principal = eval(input("Enter the initial principal: ")) apr = eval(input("Enter the annual interest rate: ")) for i in range(10): principal = principal * (1 + apr) print("The value in 10 years is:", principal) main() Python Programming, 2/e

Graphing Future Value/ Choosing Coordinates Python Programming, 2/e

Sawtooth Wave Y (-4,1) (-2,1) (0,1) (2,1) (4,1) X (-5,-1) (-3,-1) (-1,-1) (1,-1) (3,-1) (5,-1) y = (-1)x, x = -5, -4, -3, -2, -1, 0, 1, 2, 3, … Python Programming, 2/e

from graphics import. win = GraphWin() for x in range(-5, 6): y = (-1) from graphics import * win = GraphWin() for x in range(-5, 6): y = (-1) ** x y1 = (-1) ** (x+1) s = Line(Point(x, y), Point(x+1, y1)) s.draw(win) Python Programming, 2/e

from graphics import * win = GraphWin() x0 = 100 y0 = 100 for x in range(-5, 6): y = (-1) ** x y1 = (-1) ** (x+1) s = Line(Point(x + x0, y + y0), Point(x+1 + x0, y1 + y0)) s.draw(win) Python Programming, 2/e

from graphics import * win = GraphWin() x0 = 100 y0 = 100 scale = 15 for x in range(-5, 6): y = (-1) ** x y1 = (-1) ** (x+1) s = Line(Point(x * scale + x0, y * scale + y0), Point( (x+1)*scale + x0, y1*scale + y0)) s.draw(win) Python Programming, 2/e

HW: Square Wave Python Programming, 2/e

HW: Pentagon =2/5 (cos , sin ) Python Programming, 2/e

Objectives Change the default window size of GraphWin(). Perform coordinate transformations by setCoords(). Learn how to plot graphs in polar coordinates in addition to Cartesian coordinates. Python Programming, 2/e

Methods of an Object from graphics import * win = GraphWin() center = Point(100, 100) radius = 50 c1 = Circle(center, 50) c1.draw(win) # You may also draw a circle right after it is # constructed, without assigning it to a variable. # The advantage is that you cannot manipulate the # object later. Circle(center, 75).draw(win) Python Programming, 2/e

Tic-Tac-Toe (1,3) (2,3) (0,2) (3,2) (0,1) (3,1) (1,0) (2,0) Python Programming, 2/e

Code for Tic-Tac-Toe What if we want to increase from graphics import * win = GraphWin() Line( Point(1,0), Point(1,3) ).draw(win) Line( Point(2,0), Point(2,3) ).draw(win) Line( Point(0,1), Point(3,1) ).draw(win) Line( Point(0,2), Point(3,2) ).draw(win) What if we want to increase the row/column number? Python Programming, 2/e

Use a Variable for Flexibility from graphics import * win = GraphWin() n = 3 for i in range(1,n): Line( Point(i,0), Point(i,n) ).draw(win) Line( Point(0,i), Point(n,i) ).draw(win) Python Programming, 2/e

Don’t Forget to Scale Up from graphics import * win = GraphWin() x0=0 y0=0 scale=60 n = 3 for i in range(1,n): Line( Point(i*scale + x0,0 + y0), Point(i*scale+x0,n*scale+y0) ).draw(win) Line( Point(0+x0,i*scale + y0), Point(n*scale+x0,i*scale+y0) ).draw(win) Notice that I can split a command into two lines. Python Programming, 2/e

GraphWin(title, width, height) The default title is “Graphics Window”, and the default size is 200 x 200. Try to create a window with size 700 x 700. win7 = GraphWin("Large Window", 700, 700) The previous program does not work well. Python Programming, 2/e

Change the Scale scale = 233 seems to work well. Can’t the computer calculate this automatically? Python Programming, 2/e

Specify a Frame to Zoom-In (0,0) (2,0) (1,0) (1,3) (2,3) (0,2) (0,1) (3,2) (3,1) (3,3) (200,200) Python Programming, 2/e

setCoords() P.109 from graphics import * win = GraphWin() win.setCoords(0, 0, 3, 3) # lower-left x, lower-left y # upper-right x, upper right y n = 3 for i in range(1,n): Line( Point(i,0), Point(i,n) ).draw(win) Line( Point(0,i), Point(n,i) ).draw(win) Python Programming, 2/e

Line Segments from graphics import * import math win = GraphWin() n = 10 win.setCoords(-1, -1, n+1, n+1) for i in range(n+1): Line( Point(0, n-i), Point(i, 0) ).draw(win) Python Programming, 2/e

sin() (x, sinx) Try to plot the graph for cos(). from graphics import * import math win = GraphWin() win.setCoords(0, -1, 2* math.pi, 1) for degree in range(360): x = degree * math.pi / 180 y = math.sin(x) p = Point(x , y) p.draw(win) (x, sinx) Try to plot the graph for cos(). Python Programming, 2/e

Plot a Circle by Ourselves from graphics import * import math win = GraphWin() r = 1 n = 100 # granularity win.setCoords(-r-0.2, -r-0.2, r+0.2, r+0.2) # x*x + y*y = r*r # y = sqrt( r*r - x*x ) for i in range(-n, n+1): x = i / n y = math.sqrt( r*r - x*x ) Point(x, y).draw(win) Point(x, -y).draw(win) Python Programming, 2/e

Polar Coordinates Y x = r cos  y = r sin  (x,y) r  X O Python Programming, 2/e

Plot a Circle by Polar Coordinates from graphics import * import math win = GraphWin() r = 1 win.setCoords(-r-0.2, -r-0.2, r+0.2, r+0.2) # x = r * cos(theta) # y = r * sin(theta) for i in range(360): theta = i * math.pi / 180 x = r * math.cos(theta) y = r * math.sin(theta) Point(x, y).draw(win) Python Programming, 2/e

Plot an Ellipse (Oval) from graphics import * import math win = GraphWin() a = 5 b = 2 win.setCoords(-a-0.1, -a-0.1, a+0.1, a+0.1) # x = a * cos(theta) # y = b * sin(theta) for i in range(360): theta = i * math.pi / 180 x = a * math.cos(theta) y = b * math.sin(theta) Point(x, y).draw(win) Python Programming, 2/e

r = sin(3) from graphics import * import math win = GraphWin() a = 1 win.setCoords(-a-0.1, -a-0.1, a+0.1, a+0.1) # r = sin(3 * theta) for i in range(360): theta = i * math.pi / 180 r = math.sin(3 * theta) x = r * math.cos(theta) y = r * math.sin(theta) Point(x, y).draw(win) Python Programming, 2/e

Pentagon Python Programming, 2/e

Exam Write a program to plot a figure as follows. Python Programming, 2/e

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

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

Interactive Graphics The graphics module hides the underlying, low-level window management and provides two simple ways to get user input in a GraphWin. Python Programming, 2/e

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

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

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

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

Getting Mouse Clicks Python Programming, 2/e

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

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

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

Handling Textual Input Python Programming, 2/e

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

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.close() main() Python Programming, 2/e

Handling Textual Input Python Programming, 2/e

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

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

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 displayed in the output text area. Python Programming, 2/e