1 / 19

Lecture 9

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>)

kobe
Download Presentation

Lecture 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 9 Text Input and Coordinate Systems

  2. 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

  3. 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)

  4. 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.

  5. 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

  6. 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

  7. # 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)

  8. # 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()

  9. 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.

  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)

  11. #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)

  12. Draw the x axis

  13. Draw the x axis xAxis = Line(Point(-4,0), Point(4,0)) xAxis.draw(win)

  14. Draw the y axis

  15. Draw the y axis yAxis = Line(Point(0,10), Point(0,-1)) yAxis.draw(win)

  16. Make a list of points (x,x2) for x in [-3,3]

  17. 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)

  18. Draw the line segments joining successive points.

  19. Draw the line segments joining successive points. for i in range(len(plist)-1): segment = Line(plist[i],plist[i+1]) segment.draw(win)

More Related