190 likes | 349 Views
Lecture 9. Text Input and Coordinate Systems. Text Input. Sometimes we would like to use a graphical interface to get information from the user. This is called an Entry object. Create an Entry object with a command of the form e1 = Entry(< centerPoint >, <width>)
E N D
Lecture 9 Text Input and Coordinate Systems
Text Input • Sometimes we would like to use a graphical interface to get information from the user. • This is called an Entry object. • Create an Entry object with a command of the form • e1 = Entry(<centerPoint>, <width>) • e1 = Entry(Point(50,100), 10) • Creates a box centered at the pixel (50,100) that is 10 characters wide
Entry methods • In addition to the usual graphic methods: move(), draw(graphwin), undraw(), setFill(color) and clone() an Entry object can • getAnchor() – returns a clone of the center point • getText() – returns the string of text in entry box. This is either a default string or what the user has typed. • setText(string) – Sets the text in the Entry box to the string. • setFace(family) – sets the type face, ‘helvetica’, ‘courier’ ‘times roman’ and ‘arial’ are possible values for family. • setSize(point) – sets the size of the font: 5 – 36 • setStyle(style) – choose from ‘normal’, ‘bold’, ‘italic’, ‘bold italic’ • setTextColor(color)
Choosing Coordinates • Sometimes it is much more convenient to change the coordinate system in the window. • win = GraphWin() • win.setCoords(xll,yll,xur,yur) where xll is the x coordinate of the lower left point and yll is the y coordinate of the lower left point and xur is the x coordinate of the upper right point and yur is the y coordinate of the upper right point.
Example • win = GraphWin() • win.setCoords(0.0, 0.0, 3.0, 3.0) • Now the lower left corner of the window has coordinates (0,0) • The upper right corner has coordinates (3,3) Drawing a tic-tac-toe board
A graphical Interface for converting from Celsius to Fahrenheit • Create a window • Provide a text message with information • Provide a place where the user can enter a temperature in Celsius • Wait for a mouse click • Get the input • Convert from a string to a number and then convert to Fahrenheit • Change the text message and provide the answer
# convert_gui.pyw # Program to convert Celsius to Fahrenheit using a simple # graphical interface. from graphics import * def main(): win = GraphWin("Celsius Converter", 400, 300) 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)
# wait for a mouse click win.getMouse() # convert input celsius = int(input.getText()) fahrenheit = 9.0/5.0 * celsius + 32 # display output and change button output.setText("%0.1f" % fahrenheit) button.setText("Quit") # wait for click and then quit win.getMouse() win.close() main()
Class problem • Graph a quadratic function y = x2 • Algorithm • Make a window 400 by 400 pixels • Fix the coordinates so lower left is (-4,-1) and the upper right is (4,10) • Draw the x axis • Draw the y axis • Make a list of points (x,x2) for x in [-3,3] • Draw the line segments joining successive points.
#plotting quadratic functions from graphics import * win = GraphWin("A Graph", 400,400) win.setCoords(-4.0,-1.0, 4.0, 10.0) #plotting quadratic functions from graphics import * win = GraphWin("A Graph", 400,400) win.setCoords(-4.0,-1.0, 4.0, 10.0) • Make a window 400 by 400 pixels • Fix the coordinates so lower left is (-4,-1) and the upper right is (4,10)
#plotting quadratic functions from graphics import * win = GraphWin("A Graph", 400,400) win.setCoords(-4.0,-1.0, 4.0, 10.0) #plotting quadratic functions from graphics import * win = GraphWin("A Graph", 400,400) win.setCoords(-4.0,-1.0, 4.0, 10.0) • Make a window 400 by 400 pixels • Fix the coordinates so lower left is (-4,-1) and the upper right is (4,10) #plotting quadratic functions from graphics import * win = GraphWin("A Graph", 400,400) win.setCoords(-4.0,-1.0, 4.0, 10.0)
Draw the x axis xAxis = Line(Point(-4,0), Point(4,0)) xAxis.draw(win)
Draw the y axis yAxis = Line(Point(0,10), Point(0,-1)) yAxis.draw(win)
Make a list of points (x,x2) for x in [-3,3] x = -3.0 plist = [] for i in range(61): p = Point(x, x*x) plist.append(p) x = x + .1 p.draw(win)
Draw the line segments joining successive points. for i in range(len(plist)-1): segment = Line(plist[i],plist[i+1]) segment.draw(win)