Diferencia entre revisiones de «Usuario:Lmorillas/modulo programacion/python/intro graphics»
De WikiEducator
Línea 2: | Línea 2: | ||
{{:Curso_Python_DGA_2011/inmersion_python/ayuda_graphics}} | {{:Curso_Python_DGA_2011/inmersion_python/ayuda_graphics}} | ||
− | {{ | + | {{/dibuja coche}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | }} | + | |
− | {{ | + | {{/anima coche}} |
− | + | ||
− | + | ||
− | + |
Revisión de 20:53 26 oct 2011
Página clonada de http://es.wikieducator.org/Curso_Python_DGA_2011/inmersion_python/ayuda_graphics <html> <head><title>302 Found</title></head> <body>
Contenido
302 Found
</body> </html>
Basado en http://web.cs.swarthmore.edu/~adanner/cs21/f09/graphics.php
Tip: Graphics es una utilidad pensada en el aprendizaje, no para uso en producción.
- Descarga graphics.py y cópialo en el directorio donde crees tus ejercicios.
- Referencia oficial: http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html
Un ejemplo
from graphics import * Next create a new graphics window object, and then create gui objects to draw into this window: # creates new GraphWin object, 500x500 pixels in size win = GraphWin("My GUI Program", 500, 500) # creates a new Circle object centered at 50,50 with a radius of 20 pixels circ = Circle(Point(50,50), 20) # invoke the setFill method of the Circle object refered to by circ circ.setFill("red") # draw the circ object to the GraphWin win circ.draw(win)
GraphWin
# class GraphWin g = GraphWin(title, width, height) #Constructor-makes a new GraphWin object g.close() g.flush() # update drawing to graphics window g.getHeight() g.getWidth() # wait for mouse click and return Point obj representing click location g.getMouse() g.setBackground(color) #Change screen coordinates g.setCoords(lowerLeftX, lowerLeftY, upperRightX, upperRightY)
Point
# class Point p = Point(x, y) #Constructor otro_p = p.clone() # create a new Point object that is an exact copy of this one g.getX() g.getY()
Métodos comunes a todos los objetos de Graphics
objeto.draw(graphwin): # Draw the object in graphwin, which should be a # GraphWin object. A GraphicsObject may only be drawn # into one window. objeto.move(dx, dy): # move object dx units in x and dy units in y direction objeto.setFill(color): # Set interior color to color objeto.setOutline(color): # Set outline color to color objeto.setWidth(width): # Set line weight to width objeto.undraw(): # Undraw the object (i.e. hide it). objeto.clone(): # Make an exact copy of object and its properties (color, fill, etc.) == Line== <source lang="python"> l = Line(p1, p2) l.clone() l.setArrow(option) l.getCenter() l.getP1() l.getP2() l.draw(graphwin) l.move(dx, dy) l.setFill(color) l.setOutline(color) l.setWidth(width)
Rectangle
# class Rectangle r = Rectangle( p1, p2) # p1 and p2 are points for upper left lower rt
Circle
# class Circle c = Circle(p1, radius) # p1 is a Point at the center of the circle
Oval
# class Oval Oval(p1, p2) # p1 and p2 are opposite corners of bounding rectangle
Polygon
# class Polygon Polygon(<list of points>) # ej. triángulo: Polygon ([Point(10,20), Point(30,40), Point(10,60)]) getPoints() # return a list of Points in the polygon
Text
# class Text t = Text(p, text) # p is center point and text is string t = getText() t = setText(text) t = setTextColor(color) t = setFace(family) # set font face, ex. "arial" "courier" t = setSize(size) # set font size (5-36 are legal) t = setStyle(style) # set font style ex. "bold" "italic"
Entry
# class Entry e = Entry(pt, width) # pt is anchor Point (center) e.getAnchor() # returns the center Point e.getText() # returns the current text e.setText(string) # sets the text to string e.setSize(point) # sets the found size (5-36 are legal) e.setStyle(style) # sets the font style e.setTextColor(color) # sets the text color
Animation
# Animation from time import sleep sleep(0.5) #pause for 0.5 seconds
Colores
- color_rgb(r,g,b)
- nombres de colores:
alice blue 240 248 255 AliceBlue 240 248 255 antique white 250 235 215 AntiqueWhite 250 235 215 AntiqueWhite1 255 239 219 AntiqueWhite2 238 223 204 AntiqueWhite3 205 192 176 AntiqueWhite4 139 131 120 aquamarine 127 255 212 aquamarine1 127 255 212 aquamarine2 118 238 198 aquamarine3 102 205 170 aquamarine4 69 139 116 azure 240 255 255 azure1 240 255 255 azure2 224 238 238 azure3 193 205 205 azure4 131 139 139 beige 245 245 220 bisque 255 228 196 bisque1 255 228 196 bisque2 238 213 183 bisque3 205 183 158 bisque4 139 125 107 black 0 0 0 blanched almond 255 235 205 BlanchedAlmond 255 235 205 blue 0 0 255 blue violet 138 43 226 blue1 0 0 255 blue2 0 0 238 blue3 0 0 205 blue4 0 0 139 BlueViolet 138 43 226 brown 165 42 42 brown1 255 64 64 brown2 238 59 59 brown3 205 51 51 brown4 139 35 35 burlywood 222 184 135 burlywood1 255 211 155 burlywood2 238 197 145 burlywood3 205 170 125 burlywood4 139 115 85 cadet blue 95 158 160 CadetBlue 95 158 160 CadetBlue1 152 245 255 CadetBlue2 142 229 238 CadetBlue3 122 197 205 CadetBlue4 83 134 139 chartreuse 127 255 0 chartreuse1 127 255 0 chartreuse2 118 238 0 chartreuse3 102 205 0 chartreuse4 69 139 0 chocolate 210 105 30 chocolate1 255 127 36 chocolate2 238 118 33 chocolate3 205 102 29 chocolate4 139 69 19 coral 255 127 80 coral1 255 114 86 coral2 238 106 80 coral3 205 91 69 coral4 139 62 47 cornflower blue 100 149 237 CornflowerBlue 100 149 237 cornsilk 255 248 220 cornsilk1 255 248 220 cornsilk2 238 232 205 cornsilk3 205 200 177 cornsilk4 139 136 120 cyan 0 255 255 cyan1 0 255 255 cyan2 0 238 238 cyan3 0 205 205 cyan4 0 139 139 dark blue 0 0 139 dark cyan 0 139 139 dark goldenrod 184 134 11 dark gray 169 169 169 dark green 0 100 0 dark grey 169 169 169 dark khaki 189 183 107 dark magenta 139 0 139 dark olive green 85 107 47 dark orange 255 140 0 dark orchid 153 50 204 dark red 139 0 0 dark salmon 233 150 122 dark sea green 143 188 143 dark slate blue 72 61 139 dark slate gray 47 79 79 dark slate grey 47 79 79 dark turquoise 0 206 209 dark violet 148 0 211 DarkBlue 0 0 139 DarkCyan 0 139 139 DarkGoldenrod 184 134 11 DarkGoldenrod1 255 185 15 DarkGoldenrod2 238 173 14 DarkGoldenrod3 205 149 12 DarkGoldenrod4 139 101 8 DarkGray 169 169 169 DarkGreen 0 100 0 DarkGrey 169 169 169 DarkKhaki 189 183 107 DarkMagenta 139 0 139 DarkOliveGreen 85 107 47 DarkOliveGreen1 202 255 112 DarkOliveGreen2 188 238 104 DarkOliveGreen3 162 205 90 DarkOliveGreen4 110 139 61 DarkOrange 255 140 0 DarkOrange1 255 127 0 DarkOrange2 238 118 0 DarkOrange3 205 102 0 DarkOrange4 139 69 0 DarkOrchid 153 50 204 DarkOrchid1 191 62 255 DarkOrchid2 178 58 238 DarkOrchid3 154 50 205 DarkOrchid4 104 34 139 DarkRed 139 0 0 DarkSalmon 233 150 122 DarkSeaGreen 143 188 143 DarkSeaGreen1 193 255 193 DarkSeaGreen2 180 238 180 DarkSeaGreen3 155 205 155 DarkSeaGreen4 105 139 105 DarkSlateBlue 72 61 139 DarkSlateGray 47 79 79 DarkSlateGray1 151 255 255 DarkSlateGray2 141 238 238 DarkSlateGray3 121 205 205 DarkSlateGray4 82 139 139 DarkSlateGrey 47 79 79 DarkTurquoise 0 206 209 DarkViolet 148 0 211 deep pink 255 20 147 deep sky blue 0 191 255 DeepPink 255 20 147 DeepPink1 255 20 147 DeepPink2 238 18 137 DeepPink3 205 16 118 DeepPink4 139 10 80 DeepSkyBlue 0 191 255 DeepSkyBlue1 0 191 255 DeepSkyBlue2 0 178 238 DeepSkyBlue3 0 154 205 DeepSkyBlue4 0 104 139 dim gray 105 105 105 dim grey 105 105 105 DimGray 105 105 105 DimGrey 105 105 105 dodger blue 30 144 255 DodgerBlue 30 144 255 DodgerBlue1 30 144 255 DodgerBlue2 28 134 238 DodgerBlue3 24 116 205 DodgerBlue4 16 78 139 firebrick 178 34 34 firebrick1 255 48 48 firebrick2 238 44 44 firebrick3 205 38 38 firebrick4 139 26 26 floral white 255 250 240 FloralWhite 255 250 240 forest green 34 139 34 ForestGreen 34 139 34 gainsboro 220 220 220 ghost white 248 248 255 GhostWhite 248 248 255 gold 255 215 0 gold1 255 215 0 gold2 238 201 0 gold3 205 173 0 gold4 139 117 0 goldenrod 218 165 32 goldenrod1 255 193 37 goldenrod2 238 180 34 goldenrod3 205 155 29 goldenrod4 139 105 20 green 0 255 0 green yellow 173 255 47 green1 0 255 0 green2 0 238 0 green3 0 205 0 green4 0 139 0 GreenYellow 173 255 47 grey
Ejercicio dibujar coche
Moviendo el coche