Documente Academic
Documente Profesional
Documente Cultură
1 ) Window
O fereastra grafica este un loc pe ecran unde pot fi desenate elemente
grafice.
Pentru a crea o fereastra, folosim GraphWin din graphics.
GraphWin Methods
Example: win.plot(35,128,”blue")
Example: win.close()
getMouse() -Pauses for the user to click a mouse in the window and
returns where the mouse was clicked as a Point object.
click. Returns the last point where the mouse was clicked or None if the
window has not been clicked since the previous call to checkMouse or
getMouse.
Example: clickPoint=win.checkMouse()
Note: clickPoint may be None.
getKey() -Pauses for the user to type a key on the keyboard and
returns a string representing the key that was pressed.
Example: keyString=win.getKey()
checkKey()- Similar to getKey, but does not pause for the user to
press a key. Returns the last key that was pressed or " " if no key was
pressed since the previous call to checkKey or getKey. This is
particularly useful for controlling simple animation loops.
Example: keyString=win.checkKey()
2) Point
Cel mai simplu obiect din biblioteca graphics este Point.
In geometrie, un punct este o pozitie in spatiu si este localizat prin
referire la un sistem de coordonate.
Obiectul nostru graphics, Point, este similar cu cel de la geometrie:
reprezinta o pozitie in fereastra GraphWin.
>>> p.draw(win)
>>> p2.draw(win)
>>> p.getX()
50
>>> p.getY()
60
Point Methods
Example: aPoint=Point(3.5,8)
Example: yValue=aPoint.getY()
setFill (color) -Sets the interior of the object to the given color.
Example: someObject.setFill("red")
setOutline (color) -Sets the outline of the object to the given color.
Example: someObject.setOutline("yellow")
setWidth(pixels) -Sets the width of the outline of the object to the
desired number of pixels. (Does not work for Point.)
Example: some0bject.setWidth(3)
draw(aGraphWin) -Draws the object into the given GraphWin and
returns the drawn object.
Example: someObject.draw(someGraphWin)
move (dx , dy) Moves the object dx units in the x direction and dy units
in the y direction. If the object is currently drawn, the image is adjusted
to the new position.
Example: some0bject.move(10,15.5)
clone() Returns a duplicate of the object. Clones are always created in an
undrawn state. Other than that, they are identical to the cloned object.
Example:
aPolygon=Polygon([Point(1,2),Point(3,4),Point(5,
6)])
Exercitiul 3.5:
Implementati urmatoarul program:
>>> ### 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)
Output:
3.6) Text Methods
Example: message=Text(Point(3,4),"Hello!")
Example: message.setText("Goodbye!")
Example: msgString=message.getText()
Example: centerPoint=message.getAnchor( )
Example: message.setFace("arial")
setTextColor(color) -Sets the color of the text to color. Note: setFill has
the same effect.
Example: message.setTextColor("pink")
setSize (point) Changes the font size to the given point size. Sizes from
5 to 36 points are legal.
Example: message.setSize(18)
setStyle (style) -Changes font to the given style. Possible values are:
Example: message.setStyle("bold")
4) Grafica interactiva
Exercitiu 4.1
# click.py
from graphics import *
def main():
win = GraphWin("Click Me!")
for i in range(10):
p = win.getMouse()
print("You clicked at:", p.getXO, p.getY())
main()
Exercitiu 4.2
# triangle.py
# Interactive graphics program to draw a
triangle
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)
Output:
Observatii:
Nu exista o clasa triunghi. Exista in schimb clasa Polygon care accepta
un numar nelimitat de puncte si creaza un poligon folosind segmente
care conecteaza punctele intre ele, in ordinea in care sunt introduce, si
apoi conectand ultimul punct cu primul.
triangle = Polygon(p1, p2, p3)
message = Text(Point(5,0.5), "Click on
three points")
message.draw(win)
Pentru a inchide fereastra:
message.setText("Click anywhere to quit.”
5) Obiecte de tip Entry
Obiectele de tip Entry sunt afisate niste casute in care se poate introduce
text si care pot fi editate de utilizatorul programului.
Example: inputBox=Entry(Point(3,4),5)
Example: inputStr=inputBox.getText()
setText (string) Sets the text in the entry box to the given string.
Example: inputBox.setText(“32.0")
setFace(family) -Changes the font face to the given family. Possible
values are "helvetica", "courier", "times roman", and "arial".
Example: inputBox.setFace("courier")
setSize (point) - Changes the font size to the given point size. Sizes
from 5 to 36 points are legal.
Example: inputBox.setSize(12)
setStyle (style) - Changes font to the given style. Possible values are:
Example: inputBox.setStyle("italic")
Example: inputBox.setTextColor("green")
Exercitiu 5.1
Realizati o interfata grafica pentru un convertor din grade Celsius in
grade Farhenheit.
# convert_gui.pyw
# Program to convert Celsius to Fahrenheit using
a simple
# graphical interface.
Rectangle(Point(1,1.5),Point(2,2.5).draw(win
)
# wait for a mouse click
win.getMouse()
# convert input
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32
main()
Output:
Observatii
Metoda Entry deseneaza o cutie care contine text.
In urma executiei programului, se produce o casuta text in care se
introduce valoarea temperaturii.
Exercitii propuse
a+b+c
aria = s(s − a)(s − b)(s − c) unde s =
2