from Tkinter import ventanaTk cvCanvasventana width200 height200 cv

  • Slides: 13
Download presentation

from Tkinter import * ventana=Tk() cv=Canvas(ventana, width=200, height=200) cv. pack() cv. create_rectangle(20, 40, 100)

from Tkinter import * ventana=Tk() cv=Canvas(ventana, width=200, height=200) cv. pack() cv. create_rectangle(20, 40, 100) cv. create_rectangle(100, 180, 160, fill="black") ventana. mainloop()

from Tkinter import * ventana=Tk() cv=Canvas(ventana, width=200, height=200) cv. pack() cv. create_oval(20, 40, 100)

from Tkinter import * ventana=Tk() cv=Canvas(ventana, width=200, height=200) cv. pack() cv. create_oval(20, 40, 100) cv. create_oval(100, 200, fill="black") ventana. mainloop()

#circunferencias al azar from Tkinter import * import random def dibujar(): x=random. randint(0, ancho)

#circunferencias al azar from Tkinter import * import random def dibujar(): x=random. randint(0, ancho) y=random. randint(0, alto) r=random. randint(1, min(alto, ancho))/2 cv. create_oval(x-r, y-r, x+r, y+r) v=Tk() alto=200; ancho=200 b=Button(v, text="circunferencia", command=dibujar) cv=Canvas(v, width=ancho, height=alto) b. pack(); cv. pack() v. mainloop()

#actualizar angulo: sumar x (en radianes) def girar(self, x): radianes=x*math. pi/180 self. angulo +=

#actualizar angulo: sumar x (en radianes) def girar(self, x): radianes=x*math. pi/180 self. angulo += radianes #dibujar línea y reubicar tortuga def avanzar(self, x): #calcular nuevas coordenadas hf = self. h + int(x*math. cos(self. angulo)) vf = self. v - int(x*math. sin(self. angulo)) #dibujar línea self. cv. create_line(self. h, self. v, hf, vf) #actualizar ubicación de tortuga self. h=hf self. v=vf

Polígono regular de N lados de tamaño L (¿L 0 y N ? )

Polígono regular de N lados de tamaño L (¿L 0 y N ? ) #repetir n veces i=1 while i<=N: i=i+1 #avanzar tortuga en L(dibujando linea) t. avanzar(L) #girar tortuga t. girar(360. 0/N)

Nota. Existe clase predefinida en Phyton from turtle import * t=Turtle() t. left(45) i=1

Nota. Existe clase predefinida en Phyton from turtle import * t=Turtle() t. left(45) i=1 while i<=4: t. forward(100) t. left(90) i=i+1

Pelota rebotando vertical from Tkinter import * R DT VY VX = = 20

Pelota rebotando vertical from Tkinter import * R DT VY VX = = 20 1000/30 0 0 # radius in pixels # miliseg # % v = Tk() cv = Canvas(v, width=400, height=400) cv. pack() ball = cv. create_oval(180, 0, 180+2*R, fill='red') y = 0 def update_canvas() : global x, y, VY, VX cv. after(DT, update_canvas) VY = VY+0. 9 cv. move(ball, VX, VY ) #mueve el (item, dx, dy) print cv. coords(ball), VY y += VY if y + 2*R > 400: VY= -VY*0. 9 #poner pelota a raz del piso: dibujo y coordenada cv. coords(ball, 180, 400 -2*R, 180+2*R, 400) y = 400 -2*R update_canvas() v. mainloop()

Pelota rebotando x 4 lados from Tkinter import * WINW = 400 # pixels

Pelota rebotando x 4 lados from Tkinter import * WINW = 400 # pixels WINH = 400 # % R = 20 # radius in pixels DT = 1000/30 # miliseg VX = 3 # pixels/frame VY = 4 # % v = Tk() cv = Canvas(v, width=WINW, height=WINH) cv. pack() ball = cv. create_oval(0, 0, 2*R, fill='red') x = R y = R color = 'red' def update_canvas() : global x, y, VX, VY cv. after(DT, update_canvas) width = cv. winfo_width() height = cv. winfo_height() # print width, height, x, y, VX, VY cv. move(ball, VX, VY) x += VX y += VY if x + R > width or x < R : VX = -VX if y + R > height or y < R : VY= -VY update_canvas() v. mainloop()

Pelota rebotando x 4 lados interactiva from Tkinter import * def key(event) : global

Pelota rebotando x 4 lados interactiva from Tkinter import * def key(event) : global VX, VY print event. keysym if event. keysym == if VX != 0 : VX = 0 ; VY = else : VX = 3 ; VY = 'Up' : VY = -abs(VY) 'Down' : VY = abs(VY) 'Left' : VX = -abs(VX) 'Right' : VX = abs(VX) 'space' : 0 4 def mouse(event) : global x, y, color print x, y, event. x, event. y if (x-event. x)**2+(y-event. y)**2<=R*R : if color=='red' : color = 'blue‘ else : color = 'red’ cv. itemconfig(ball, fill=color) update_canvas() cv. bind('<Key>', key) cv. focus_set() cv. bind('<Button-1>', mouse) v. mainloop()