martes, 7 de noviembre de 2017

Practicas de Python 3D

Practica 1: Cubo 3D con Pygame

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )


def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)


main()





Practica 2: Piramide 3D con Pygame

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (0,0,1),
    (-.5, -.5, 0),
    (-.5, .5, 0),
    (.5, .5, 0),
    (.5, -.5, 0),

    )

edges = (
    (1,2),
    (1,4),
    (1,0),
    (3,2),
    (3,4),
    (3,0),
    (4,0),
    (2,0)
    )


def Piramide():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Piramide()
        pygame.display.flip()
        pygame.time.wait(10)


main()



Practica 3: Graficas con matplotlib

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

xpos = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
ypos = [2,3,4,5,1,6,2,1,7,2,3,5,1,3,2]
num_elements = len(xpos)
zpos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dx = np.ones(15)
dy = np.ones(15)
dz = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

ax1.bar3d(xpos, ypos, zpos, dx, dy, dz, color='#00ceaa')
plt.show()



martes, 31 de octubre de 2017

cubito 3D con Pygame

http://codentronix.com/2011/05/12/rotating-3d-cube-using-python-and-pygame/

martes, 24 de octubre de 2017

Dibujar Con Turtle, Botones y Funciones. Clicks de Ratón

# -*- coding: utf-8 -*-
import turtle
from tkinter import *
from tkinter import simpledialog


v0 = Tk()
v0.title("Ventana madre")
v0.config(bg="black")
v0.geometry("500x400+80+150")
turtle.setup(600,500,600,90)
turtle.screensize(550,400)


global frente
frente=1
global angulo
angulo=0
global GrAcom
GrAcom=0
global linea
linea=1
global colorDelinea
colorDelinea="black"
#Dibujar a mano
b1 = "levantado"xAntigua, yAntigua = None, None
def click(event):
    global b1
    b1 = "abajo"
def soltarclick(event):
    global b1, xAntigua, yAntigua
    b1 = "arriba"    
    xAntigua = None
    yAntigua = None
def mover(event):
    if b1 == "abajo":
        global xAntigua, yAntigua
        if xAntigua != None and yAntigua != None:
            papel.create_line(xAntigua, yAntigua, event.x-300, event.y-250, width=linea,fill=colorDelinea, smooth=TRUE)
        xAntigua = event.x-300
        yAntigua = event.y-250
        print("x="+str(xAntigua+300)+" y="+str(yAntigua+250))



papel= turtle.getcanvas()
papel.bind("<Motion>", mover)
papel.bind("<ButtonPress>", click)
papel.bind("<ButtonRelease>", soltarclick)


b1 = Button(v0, text="Cambiar Fondo", command=lambda: fond())
b1.grid(row=1,column=1)
b2 = Button(v0,text="Pixeles", command=lambda: cantDelante())
b2.grid(row=2,column=2)
b3 = Button(v0, text="Avanzar", command=lambda: delante())
b3.grid(row=2,column=1)
b4 = Button(v0, text="Girar Derecha",command=lambda: giraDerecha())
b4.grid(row=3,column=1)
b5 = Button(v0, text="Grados", command=lambda:angDerecha())
b5.grid(row=3,column=4)
b6 = Button(v0, text="Girar Izq.",command=lambda: giraIzquierda())
b6.grid(row=3,column=2)
b7 = Button(v0, text="Color de relleno",command=lambda: rellenar())
b7.grid(row=4,column=1)
b8 = Button(v0, text="Iniciar Relleno",command=lambda: inicioRelleno())
b8.grid(row=4,column=3)
b9 = Button(v0, text="Terminar Relleno",command=lambda: finRelleno())
b9.grid(row=4,column=4)
b10 = Button(v0, text="Limpiar",command=lambda: limpiar())
b10.grid(row=5,column=1)
b11 = Button(v0, text="Reiniciar Puntero",command=lambda: Punto0())
b11.grid(row=6,column=1)
b12 = Button(v0, text="Tamaño de lineas",command=lambda: cursorTamaño())
b12.grid(row=7,column=1)
b13 = Button(v0, text="Color de lineas",command=lambda: colorLinea())
b13.grid(row=8,column=1)

label1= Label(v0,text="="+str(frente))
label1.grid(row=2,column=3)
label2= Label(v0,text="="+str(angulo))
label2.grid(row=3,column=5)
label3= Label(v0, text="Angulo actual=0",)
label3.grid(row=3,column=3)
label4= Label(v0, text="Color de relleno=...")
label4.grid(row=4,column=2)
label5= Label(v0, text="Tamaño de linea=  ...")
label5.grid(row=7,column=2)
label6= Label(v0, text="Color de linea=  ...")
label6.grid(row=8,column=2)


fonde = turtle.Screen()
turtle.colormode(255)

def delante():
    turtle.forward(frente)

def cantDelante():
    global frente
    frente = simpledialog.askinteger("Distancia a Avanzar", "Escribe los pixeles a recorrer:")
    label1.config(text="="+str(frente))

def fond():
    fondo = simpledialog.askstring("Color de fondo", "Escribe el color del fondo:")
    fonde.bgcolor(fondo)

def giraDerecha():
    turtle.right(angulo)
    global GrAcom
    GrAcom=GrAcom-angulo
    if GrAcom<0:
        GrAcom=GrAcom+360
    label3.config(text="Angulo actual="+str(GrAcom))

def angDerecha():
    global angulo
    angulo = simpledialog.askinteger("Angulo", "Escribe los grados a girar hacia la derecha:")
    label2.config(text="=" + str(angulo))

def giraIzquierda():
    turtle.left(angulo)
    global GrAcom
    GrAcom = GrAcom + angulo
    if GrAcom > 360:
        GrAcom = GrAcom - 360
    label3.config(text="Angulo actual=" + str(GrAcom))

def angIzquierda():
    global angulo
    angulo = simpledialog.askinteger("Angulo", "Escribe los grados a girar hacia la derecha:")
    label2.config(text="=" + str(angulo))

def rellenar():
    color=simpledialog.askstring("Relleno","Escribe color de relleno")
    turtle.fillcolor(color)
    label4.config(text="Color de relleno="+str(color))

def inicioRelleno():
turtle.begin_fill() def finRelleno(): turtle.end_fill() def limpiar(): turtle.clear() papel.delete("all") def Punto0(): turtle.penup() turtle.goto(0,0) turtle.pendown() def cursorTamaño(): global linea linea=simpledialog.askstring("Tamaño de contorno","Escribe el grosor de contorno") turtle.pensize(linea) label5.config(text="Tamaño de linea= "+str(linea)) def colorLinea(): global colorDelinea colorDelinea=simpledialog.askstring("Color de Lineas: ","Escribe el color de las lineas:") turtle.pencolor(colorDelinea) label6.config(text="Tamaño de linea= " + str(colorDelinea)) v0.mainloop()


lunes, 16 de octubre de 2017

Turtle Ejemplos y Barquito

from turtle import *

setup(640, 480, 0, 0) #crear ventana turtledone()




from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
done()


from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
goto(100, -50)
goto(50, -50)
done()



from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
sety(-50)
setx(50)
setx(0)
sety(50)
setx(100)
goto(50,-50)
goto(0,0)
goto(50,50)
goto(100,0)
goto(50,-50)
hideturtle()
done()

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
penup()
goto(100, -50)
pendown()
goto(50, -50)
done()



from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
pensize(4)
goto(100, -50)
pensize(8)
goto(50, -50)
done()

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(255)

pencolor(255, 0, 0)
goto(100, 50)
pencolor(0, 255, 0)
goto(100, -50)
pencolor(0, 0, 255)
goto(50, -50)
done()


from turtle import *

setup(600, 600, 0, 0)
screensize(1300, 1300)
penup()
goto(50,300)
pendown()
fillcolor("brown")
begin_fill()
goto(150,200)
goto(400,200)
goto(500,300)
end_fill()
fillcolor("pink")
goto(300,300)
begin_fill()
goto(300,500)
goto(450,350)
goto(300,350)
end_fill()
fillcolor("pink")
goto(300,500)
goto(275,500)
goto(275,600)
goto(225,550)
goto(275,550)
goto(275,500)
goto(250,500)
begin_fill()
goto(100,350)
goto(250,350)
end_fill()
goto(250,500)
goto(250,300)
goto(50,300)
penup()
goto(300,300)
fillcolor("#663300")
begin_fill()
goto(300,500)
goto(250,500)
goto(250,300)
end_fill()
done()






lunes, 9 de octubre de 2017

Huevo Estrellado/Flor Sencilla Con Funciones y Turtle

from turtle import *
def Circulo(n):
 for i in range(n):
    right(1)
    forward(1)
def flor():
    for i in range(4):
        Circulo(180)
        left(90)
        forward(50)



color('red','pink')
Circulo(360)
penup()
left(90)
forward(50)
pendown()
right(45)
flor()
done()


martes, 26 de septiembre de 2017

Figura Mario Bros 8Bits. Tkinter (Canvas)

from tkinter import *

VentanaP = Tk()
VentanaP.title("Poligono")
poly = Canvas(VentanaP, width=210, heigh=210, bg='White')
poly.pack()
poly.create_polygon(50,20,100,20,100,30,130,30,130,40,110,40,110,50,130,50,130,60,140,60,140,70,130,70,130,80,120,80,120,90,100,90,100,100,130,100,130,110,140,110,140,150,120,150,120,160,130,160,130,170,140,170,140,180,100,180,100,160,90,160,90,150,70,150,70,160,60,160,60,180,20,180,20,170,30,170,30,160,40,160,40,150,20,150,20,110,30,110,30,100,40,100,40,90,50,90,50,80,30,80,30,50,40,50,40,30,50,30, fill="red", outline='white')
VentanaP.config(bg="blue")
VentanaP.geometry("210x210")
VentanaP.mainloop()


Figuras, botones, poligonos y condicion IF (Aportacion de compañero Aquino + Figuras) Actualizado

# -*- coding: utf-8 -*-

from tkinter import *
v0 = Tk()
v0.title("Ventana madre")
v0.config(bg="black")
v0.geometry("1400x200")

def mostrar(num):
    v1 = Toplevel(v0)
    v1.title("Ventana hija")
    v1.protocol("WM_DELETE_WINDOW", "onexit")
    v1.config(bg="black")
    v1.geometry("500x500")
    if num == 1:
     hola = Canvas(v1, width=300, height=210, bg='white')
     hola.pack(expand=YES, fill=BOTH)
     hola.create_oval(10, 10, 200, 200, width=5, fill='blue')
     b3 = Button(hola, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
    elif num == 2:
     canvas = Canvas(v1,width=300, height=210, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_rectangle(10, 10, 200, 200, width=5, fill='red')
     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
    elif num == 3:
     canvas2 = Canvas(v1,width=300, height=210, bg='white')
     canvas2.pack(expand=YES, fill=BOTH)
     canvas2.create_line(0, 200, 200, 0, width=10, fill='green')
     canvas2.create_line(0, 0, 200, 200, width=10, fill='red')
     b3 = Button(canvas2, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
     v1.deiconify()
    elif num == 4:
     canvas = Canvas(v1, width=300, height=200, bg='white')

     canvas.pack(expand=YES, fill=BOTH)
     xy = 10, 10, 190, 190
     canvas.create_arc(xy, start=0, extent=270, fill='gray60')

     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
     v1.deiconify()
    elif num == 5:
     canvas123 = Canvas(v1,width=300, height=200, bg='white')
     canvas123.pack(expand=YES, fill=BOTH)
     canvas123.create_arc(10, 10, 190, 190, start=270, extent=90, fill='gray90')
     b3 = Button(canvas123, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
     v1.deiconify()

    elif num == 6:

     canvas = Canvas(v1, width=300, height=200, bg='white')

     canvas.pack(expand=YES, fill=BOTH)
     xy = 10, 10, 190, 190
     canvas.create_arc(xy, start=0, extent=270, fill='gray60')

     canvas.create_arc(10, 10, 190, 190, start=270, extent=90, fill='gray90')
     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()
    elif num == 7:

     canvas = Canvas(v1, width=300, height=200, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_text(150,70,fill="Red",font="Times",text="Hola \nmundo")
     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
    elif num == 8:

     canvas = Canvas(v1, width=300, height=200, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(40, 40, 40, 140, 140, 140, 140, 100,fill="lightblue", outline="brown", width=6)
     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()
    elif num == 9:

     canvas = Canvas(v1, width=300, height=200, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(100,110,100,270,150,270,150,190,220,190,230,280,290,280,300,100,240,90,230,160,160,160,140,100, fill="lightblue", outline="brown", width=6)
     b3 = Button(canvas, text="ocultar", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 10:

     canvas = Canvas(v1, width=300, height=200, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(130,20,150,90,210,90,160,130,180,200,130,160,80,200,100,130,50,90,110,90, fill="lightblue", outline="brown", width=6)
     b3 = Button(canvas, text="ocultar Dibujo de David Maldonado", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 11:

     canvas = Canvas(v1, width=210, heigh=210, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(30, 20, 20, 80, 40, 100, 80, 80, 80, 120, 100, 120, 100, 80, 130, 80, 130, 110, 150, 110, 150,
                         50, 170, 10, 110, 40, 70, 40, 50, 25, fill="red", outline='white')
     b3 = Button(canvas, text="ocultar Dibujo de Emanuel Horta", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 12:

     canvas = Canvas(v1, width=390, height=350, bg='black')
     canvas.pack(expand=YES, fill=BOTH)
     puntos = [00, 20, 60, 20, 80, 80, 120, 80, 160, 20, 220, 40, 180, 40, 140, 80, 160, 100, 160, 120, 160, 140, 140,
               160, 180, 180, 200, 200, 200, 240, 180, 280, 140, 300, 180, 320, 180, 340, 120, 340, 100, 320, 80, 340,
               20, 340, 20, 320, 60, 300, 20, 280, 00, 240, 00, 200, 20, 180, 60, 160, 40, 140, 40, 120, 40, 100, 60,
               80, 40, 40]
     canvas.create_polygon(puntos, fill="white", outline="white", width=9)
     b3 = Button(canvas, text="ocultar Lourdes Brito", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 13:

     canvas = Canvas(v1,  width=700, height=600, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon((200, 400), (100, 500), (100, 600), (200, 500), (300, 600), (300, 500), (200, 400),
                             (300, 300), fill="green", outline="brown", width=2)
     canvas.create_polygon((200, 200), (300, 100), (500, 100), (500, 300), (400, 400), (300, 300), fill="red", outline="brown", width=2)
     b3 = Button(canvas, text="ocultar Edgar Emanuel", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 14:
     canvas = Canvas(v1)
     canvas.config(width=400, height=600,
                   bg='white')  # Lienzo con las medidas y color sobre el que se dibujará la figura
     canvas.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura

     canvas.create_polygon(52.5, 14.5, 37, 59.1, 51, 59.1, fill="#5C554E", outline="#5C554E", width=1)
     canvas.create_polygon(19.3, 56.8, 58, 59.1, 67, 91, 37.5, 83, fill="#C3BAB5", outline="#C3BAB5", width=1)
     canvas.create_polygon(58, 57.8, 78, 77, 71, 103, fill="#D4CCC1", outline="#D4CCC1", width=1)
     canvas.create_polygon(37.5, 83, 67.4, 91, 71, 103, fill="#998C8A", outline="#998C8A", width=1)
     canvas.create_polygon(71, 103, 71, 104.8, 59.1, 104.8, 53.2, 91, fill="#665B57", outline="#665B57", width=1)

     canvas.create_polygon(86, 105.6, 98.2, 148.6, 47, 133.6, 24.2, 103.6, fill="#C3BAB5", outline="#C3BAB5", width=1)
     canvas.create_polygon(98.2, 148.6, 102, 165.2, 47, 133.6, fill="#9B8D8A", outline="#9B8D8A", width=1)
     canvas.create_polygon(86, 105.6, 124, 169, 120, 196, 110.8, 196, fill="#D5CDC2", outline="#D5CDC2", width=1)
     canvas.create_polygon(102, 165.2, 110.8, 196, 99, 196, 80.6, 153, fill="#605550", outline="#605550", width=1)

     canvas.create_polygon(139.5, 197, 147, 241, 71, 220.4, 46, 193, fill="#C3BAB5", outline="#C3BAB5", width=1)
     canvas.create_polygon(147, 241, 150, 261.4, 71, 220.4, fill="#968B87", outline="#968B87", width=1)
     canvas.create_polygon(139.5, 197, 193, 274.2, 189, 307.8, 176, 321, 161.6, 321, fill="#D4CCC1", outline="#D4CCC1",
                           width=1)
     canvas.create_polygon(150, 261.4, 161.6, 321, 126.8, 249, fill="#605551", outline="#605551", width=1)

     canvas.create_polygon(199.4, 307.8, 189, 307.8, 176, 321, 186.6, 461, 184, 448.8, 171, 479.5, 199.4, 503,
                           fill="#C2AD9C", outline="#C2AD9C", width=1)
     canvas.create_polygon(176, 321, 161.6, 321, 186.6, 461, fill="#615652", outline="#615652", width=1)
     canvas.create_polygon(161.6, 321, 136, 359.4, 177.2, 409, fill="#D9C6B7", outline="#D9C6B7", width=1)
     canvas.create_polygon(144.4, 369.8, 139.6, 384.6, 160, 389.4, fill="#443232", outline="#443232", width=1)
     canvas.create_polygon(139.6, 384.6, 160, 389.4, 177.2, 409, 169.2, 450, fill="#826E65", outline="#826E65", width=1)
     canvas.create_polygon(171, 479.5, 180.6, 497.8, 191.2, 496.8, fill="#463334", outline="#463334", width=1)
     canvas.create_polygon(177.2, 409, 164.8, 475, 176.5, 511, 199.4, 522, 199.4, 502.8, 191.2, 496.8, 180.6, 497.8,
                           171, 479.5, 184, 448.8, fill="#9B7F79", outline="#9B7F79", width=1)

     canvas.create_polygon(151.8, 335.4, 109.6, 280, 142.2, 349.2, fill="#5F5150", outline="#5F5150", width=1)
     canvas.create_polygon(109.6, 280, 70.3, 266, 94.3, 329, 131.3, 326, fill="#483636", outline="#483636", width=1)
     canvas.create_polygon(94.3, 329, 137, 336, 132, 326.8, fill="#C2AF9D", outline="#C2AF9D", width=1)
     canvas.create_polygon(115, 333, 136, 359.4, 142.2, 349.2, 137, 336, fill="#826E65", outline="#826E65", width=1)

     # Lado inverso
     canvas.create_polygon(346.5, 14.5, 347.5, 59.1, 361, 59.1, fill="#2E2621", outline="#2E2621", width=1)
     canvas.create_polygon(379.4, 56.8, 341.8, 59.1, 332, 91, 361, 83, fill="#908782", outline="#908782", width=1)
     canvas.create_polygon(341.5, 57.8, 324, 75, 327.6, 103, fill="#A29B8F", outline="#A29B8F", width=1)
     canvas.create_polygon(361, 83, 332.4, 91, 329, 103, fill="#685D59", outline="#685D59", width=1)
     canvas.create_polygon(329, 103, 326.8, 104.8, 340, 104.8, 345.6, 91, fill="#2D2220", outline="#2D2220", width=1)

     canvas.create_polygon(313, 105.6, 301, 148.6, 352, 133.6, 374.5, 103.6, fill="#908782", outline="#908782", width=1)
     canvas.create_polygon(301, 148.6, 297, 165.2, 352, 133.6, fill="#625755", outline="#625755", width=1)
     canvas.create_polygon(313, 105.6, 274, 169, 279, 196, 288, 196, fill="#A1998E", outline="#A1998E", width=1)
     canvas.create_polygon(297, 165.2, 288, 196, 300, 196, 318, 153, fill="#2D221E", outline="#2D221E", width=1)

     canvas.create_polygon(260, 197, 252, 241, 331, 220.4, 352.4, 193, fill="#908782", outline="#908782", width=1)
     canvas.create_polygon(252, 241, 249, 261.4, 331, 220.4, fill="#645955", outline="#645955", width=1)
     canvas.create_polygon(260, 197, 205, 274.2, 209.8, 307.8, 223, 321, 238, 321, fill="#A1998E", outline="#A1998E",
                           width=1)
     canvas.create_polygon(249, 261.4, 238.6, 321, 271.8, 249, fill="#2D221E", outline="#2D221E", width=1)

     canvas.create_polygon(199.4, 307.8, 209.8, 307.8, 223, 321, 212.6, 461, 215, 448.8, 228, 479.5, 199.4, 503,
                           fill="#8E7968", outline="#8E7968", width=1)
     canvas.create_polygon(223, 321, 238.6, 321, 212.6, 461, fill="#302722", outline="#302722", width=1)
     canvas.create_polygon(238, 321, 262.8, 359.4, 222.5, 409, fill="#A69384", outline="#A69384", width=1)
     canvas.create_polygon(254.4, 369.8, 259.6, 384.6, 238, 389.4, fill="#120001", outline="#120001", width=1)
     canvas.create_polygon(259.6, 384.6, 238, 389.4, 222.5, 409, 229.5, 450, fill="#4F3B32", outline="#4F3B32", width=1)
     canvas.create_polygon(228, 479.5, 218.3, 497.8, 207.5, 496.8, fill="#180A04", outline="#180A04", width=1)
     canvas.create_polygon(222.5, 409, 234, 475, 223, 511, 199.4, 522, 199.4, 502.8, 207.5, 496.8, 218.3, 497.8, 228,
                           479.5, 215, 448.8, fill="#674C45", outline="#674C45", width=1)

     canvas.create_polygon(247, 335.4, 290.6, 280, 256.8, 349.2, fill="#281F1D", outline="#281F1D", width=1)
     canvas.create_polygon(290.6, 280, 328, 266, 304.8, 329, 267.5, 326, fill="#140202", outline="#140202", width=1)
     canvas.create_polygon(304.8, 329, 262.5, 336, 267.5, 326.8, fill="#8D7B66", outline="#8D7B66", width=1)
     canvas.create_polygon(283, 333, 262.8, 359.4, 256.8, 349.2, 262.5, 336, fill="#4D3930", outline="#4D3930", width=1)

     b3 = Button(canvas, text="ocultar Dibujo de David Alejandro Mendoza", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)

     v1.deiconify()

    elif num == 15:
     puntos = [226, 42, 198, 42, 170, 50, 141, 71, 127, 60, 108, 52, 94, 57, 85, 71, 85, 85, 95, 99, 108, 106, 120, 109,
               127, 113, 127, 123, 142, 119, 161, 119, 178, 122, 189, 134, 192, 148, 189, 161, 176, 166, 160, 165, 142,
               162, 156, 178, 170, 192, 192, 198, 207, 198, 198, 212, 170, 209, 151, 205, 132, 202, 113, 195, 108, 180,
               99, 164, 80, 153, 57, 156, 38, 170, 34, 183, 35, 198, 42, 212, 56, 221, 71, 226, 85, 221, 104, 212, 127,
               223, 152, 226, 175, 232, 189, 234, 184, 250, 184, 270, 184, 283, 190, 297, 196, 306, 184, 321, 180, 334,
               178, 346, 180, 353, 188, 372, 212, 390, 194, 402, 181, 411, 170, 425, 170, 443, 176, 456, 190, 467, 208,
               468, 227, 456, 275, 419, 276, 404, 269, 393, 252, 383, 236, 382, 221, 371, 206, 361, 201, 350, 203, 340,
               209, 333, 221, 326, 237, 326, 255, 322, 270, 314, 280, 305, 297, 291, 311, 288, 335, 288, 354, 293, 368,
               301, 378, 311, 386, 326, 403, 330, 411, 330, 462, 265, 461, 240, 450, 230, 435, 226, 421, 226, 406, 230,
               396, 240, 380, 270, 354, 265, 332, 260, 311, 263, 297, 263, 283, 263, 278, 250, 264, 226, 283, 226, 297,
               221, 312, 212, 330, 198, 346, 198, 363, 198, 375, 188, 382, 175, 386, 160, 382, 141, 362, 127, 334, 127,
               326, 133, 312, 148, 312, 163, 315, 180, 304, 192, 290, 204, 273, 206, 255, 205, 241, 204, 234, 198, 255,
               189, 269, 180, 278, 166, 286, 151, 291, 132, 292, 113, 289, 99, 283, 85, 280, 74, 269, 63, 255, 51, 234,
               42]

     canvas = Canvas(v1, width=500, height=500, bg='white')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(puntos, fill="black", outline="white", width=6)
     b3 = Button(canvas, text="ocultar Ricardo Ledezma", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
     v1.deiconify()

    elif num == 16:
     canvas = Canvas(v1, width=210, heigh=210, bg='White')
     canvas.pack(expand=YES, fill=BOTH)
     canvas.create_polygon(50, 20, 100, 20, 100, 30, 130, 30, 130, 40, 110, 40, 110, 50, 130, 50, 130, 60, 140, 60, 140,
                         70, 130, 70, 130, 80, 120, 80, 120, 90, 100, 90, 100, 100, 130, 100, 130, 110, 140, 110, 140,
                         150, 120, 150, 120, 160, 130, 160, 130, 170, 140, 170, 140, 180, 100, 180, 100, 160, 90, 160,
                         90, 150, 70, 150, 70, 160, 60, 160, 60, 180, 20, 180, 20, 170, 30, 170, 30, 160, 40, 160, 40,
                         150, 20, 150, 20, 110, 30, 110, 30, 100, 40, 100, 40, 90, 50, 90, 50, 80, 30, 80, 30, 50, 40,
                         50, 40, 30, 50, 30, fill="red", outline='white')
     b3 = Button(canvas, text="ocultar Figura de Mario", command=lambda: ejecutar(ocultar(v1)))
     b3.grid(row=1, column=3)
     v1.deiconify()

def ocultar(ventana):ventana.destroy()
def ejecutar(f): v0.after(200, f)
b1 = Button(v0, text="Abrir ventana con Circulo", command=lambda: ejecutar(mostrar(1)) )
b1.grid(row=1, column=1)
b2 = Button(v0, text="Abrir ventana con cuadrado", command=lambda: ejecutar(mostrar(2)))
b2.grid(row=1, column=2)
b3 = Button(v0, text="Abrir ventana con linea", command=lambda: ejecutar(mostrar(3)))
b3.grid(row=1, column=3)
b4 = Button(v0, text="Abrir ventana con arco", command=lambda: ejecutar(mostrar(4)))
b4.grid(row=1, column=4)
b5 = Button(v0, text="Abrir ventana con quesito", command=lambda: ejecutar(mostrar(5)))
b5.grid(row=1, column=5)
b6 = Button(v0, text="Abrir ventana con quesito y arco", command=lambda: ejecutar(mostrar(6)))
b6.grid(row=1, column=6)
b7 = Button(v0, text="Abrir ventana con texto", command=lambda: ejecutar(mostrar(7)))
b7.grid(row=1, column=7)
b8 = Button(v0, text="Abrir ventana con poligono", command=lambda: ejecutar(mostrar(8)))
b8.grid(row=1, column=8)
b9 = Button(v0, text="Abrir ventana con poligono 2", command=lambda: ejecutar(mostrar(9)))
b9.grid(row=1, column=9)
b10 = Button(v0, text="Estrella", command=lambda: ejecutar(mostrar(10)))
b10.grid(row=3, column=2)
b11 = Button(v0, text="Perrito", command=lambda: ejecutar(mostrar(11)))
b11.grid(row=2, column=1)
b12 = Button(v0, text="Conejo", command=lambda: ejecutar(mostrar(12)))
b12.grid(row=2, column=2)
b13 = Button(v0, text="Manzana", command=lambda: ejecutar(mostrar(13)))
b13.grid(row=2, column=3)
b14 = Button(v0, text="Antilope", command=lambda: ejecutar(mostrar(14)))
b14.grid(row=2, column=4)
b15 = Button(v0, text="Mr. Game & Watch", command=lambda: ejecutar(mostrar(15)))
b15.grid(row=2, column=5)
b16 = Button(v0, text="Mario", command=lambda: ejecutar(mostrar(16)))
b16.grid(row=3, column=1)

v0.mainloop()


miércoles, 20 de septiembre de 2017

Flechita con TKinter (Poligonos)

Flecha

from tkinter import *

VentanaP = Tk()
VentanaP.title("Poligono")
poly = Canvas(VentanaP, width=210, heigh=210, bg='White')
poly.pack()
poly.create_polygon(45, 0, 40, 88, 64, 68, 88, 116, 122, 88, 100, 52, 124, 32, fill='blue', outline='white')
VentanaP.config(bg="blue")
VentanaP.geometry("210x210")
VentanaP.mainloop()


jueves, 14 de septiembre de 2017

Figuras, Botones, Ventanas Hijas

Ejercicios de distintas figuras en diferentes ventanas, con botones, funciones etc..


from tkinter import *

VentanaP = Tk()
Hija1 = Toplevel(VentanaP)
Hija2 = Toplevel(VentanaP)
Hija3 = Toplevel(VentanaP)
Hija4 = Toplevel(VentanaP)
Hija5 = Toplevel(VentanaP)
Hija6 = Toplevel(VentanaP)
VentanaP.title("Figuras")
Hija1.title("Circulo")
Hija2.title("Rectangulo")
Hija3.title("Lineas")
Hija4.title("Quesito")
Hija5.title("Arco")
Hija6.title("Grafico")
Hija1.protocol("WM_DELETE_WINDOW", "onexit")
Hija2.protocol("WM_DELETE_WINDOW", "onexit")
Hija3.protocol("WM_DELETE_WINDOW", "onexit")
Hija4.protocol("WM_DELETE_WINDOW", "onexit")
Hija5.protocol("WM_DELETE_WINDOW", "onexit")
Hija6.protocol("WM_DELETE_WINDOW", "onexit")


def mostrar(ventana): ventana.deiconify()


def ocultar(ventana): ventana.withdraw()


def ejecutar(f): VentanaP.after(200, f)


VentanaP.config(bg="blue")
VentanaP.geometry("468x300")


def circulo(ventana):
    Hija1.deiconify()
    circulo = Canvas(Hija1, width=210, height=210, bg="white")
    circulo.pack()
    cuadro = circulo.create_oval(10, 10, 200, 200, width=3, fill="blue")


def rectangulo(ventana):
    Hija2.deiconify()
    rectangulo = Canvas(Hija2, width=210, height=210, bg="white")
    rectangulo.pack(expand=YES, fill=BOTH)
    rectangulo.create_rectangle(10, 10, 200, 200, width=3, fill='black')


def lineas(ventana):
    Hija3.deiconify()
    linea = Canvas(Hija3, width=210, height=210, bg='white')
    linea.pack(expand=YES, fill=BOTH)
    linea.create_line(0, 200, 200, 0, width=10, fill='black', dash=(4, 4))
    linea.create_line(0, 0, 200, 200, width=10, fill='black', dash=(4, 4))

def quesito(ventana):
    Hija4.deiconify()
    quesito = Canvas(Hija4,width=300, height=200, bg='white')
    quesito.pack(expand=YES, fill=BOTH)
    quesito.create_arc(10, 10, 190, 190, start=270, extent=90, fill='gray90')


def arco(ventana):
    Hija5.deiconify()
    arco = Canvas(Hija5, width=300, height=200, bg='white')
    arco.pack(expand=YES, fill=BOTH)
    xy = 10, 10, 190, 190    arco.create_arc(xy, start=0, extent=270, fill='gray60')


def grafico(ventana):
    Hija6.deiconify()
    arco = Canvas(Hija6, width=300, height=200, bg='white')
    arco.pack(expand=YES, fill=BOTH)
    xy = 10, 10, 190, 190    arco.create_arc(xy, start=0, extent=270, fill='gray60')
    arco.create_arc(10, 10, 190, 190, start=270, extent=90, fill='gray90')


boton2 = Button(Hija6, text="Cerrar", relief=SOLID, bg='green', cursor="box_spiral", command=lambda: ejecutar(ocultar(Hija6)))
boton2.pack()

boton6 = Button(Hija4, text="Cerrar", relief=SOLID, bg='green', cursor="box_spiral", command=lambda: ejecutar(ocultar(Hija4)))
boton6.pack()
boton4 = Button(Hija5, text="Cerrar", relief=SOLID, bg='green', cursor="box_spiral", command=lambda: ejecutar(ocultar(Hija5)))
boton4.pack()

boton3 = Button(Hija1, text="Cerrar", relief=SOLID, bg='green', cursor="umbrella", command=lambda: ejecutar(ocultar(Hija1)))
boton3.pack()

boton5 = Button(Hija2, text="Cerrar", relief=SOLID, bg='red', cursor="pencil", command=lambda: ejecutar(ocultar(Hija2)))
boton5.pack()
boton2 = Button(Hija3, text="Cerrar", relief=SOLID, bg='blue', cursor="umbrella", command=lambda: ejecutar(ocultar(Hija3)), fg='#ffffff')
boton2.pack()

b1 = Button(VentanaP, text="Mostrar circulo", relief=FLAT, bg='white', cursor="sizing", command=lambda: ejecutar(circulo(Hija1)))
b1.grid(row=1, column=3)

b2 = Button(VentanaP, text="Mostrar rectangulo", relief=FLAT, bg='black', cursor="hand1",            command=lambda: ejecutar(rectangulo(VentanaP)), fg='#ffffff')
b2.grid(row=1, column=2)

b3 = Button(VentanaP, text="Mostrar lineas", relief=FLAT, bg='white', cursor="hand2", command=lambda: ejecutar(lineas(VentanaP)))
b3.grid(row=1, column=1)

b4 = Button(VentanaP, text="Mostrar Quesito", relief=FLAT, bg='white', cursor="hand2", command=lambda: ejecutar(quesito(VentanaP)))
b4.grid(row=1, column=4)

b5 = Button(VentanaP, text="Mostrar Arco", relief=FLAT, bg='white', cursor="hand2", command=lambda: ejecutar(arco(VentanaP)))
b5.grid(row=1, column=5)
b6 = Button(VentanaP, text="Mostrar Grafico", relief=FLAT, bg='white', cursor="hand2", command=lambda: ejecutar(grafico(VentanaP)))
b6.grid(row=2, column=1)

Hija3.withdraw()
Hija2.withdraw()
Hija1.withdraw()
Hija4.withdraw()
Hija5.withdraw()
Hija6.withdraw()
VentanaP.mainloop()


Turtle en Python

Dibujos con Turtle

Para turtle se puede controlar la fleca y e dibujo con unas simples instrucciones
Ejemplo:

from turtle import *  #Importa la libreria de turtle
def curvemove():    #Define la funcion de movimiento curveado
 for i in range(200):       #repite 200 veces
    right(1)                #mueve la flecha de turtle 1 grado hacia la derecha
    forward(1)              #Mueve la flecha de turtle 1 grado a
color('red','pink')         #define el color de linea, Relleno
begin_fill()                #Rellena la figura
left(140)                   #Gira la fleca 140 grados a la izquierda
forward(111.65)             #Avanza hacia adelante 111.65 pixeles
curvemove()                 #Llama la funcion de movimiento de curva
left(120)                   #Gira acia la izquierda 120 grados
curvemove()                 #movimiento curva
forward(111.65)             #Adelante 111.65 pixeles
end_fill()                  #Termina de rellenar
done()                      #Finaliza




martes, 12 de septiembre de 2017

Juego de Gato Python 3

Juego de Gato

from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog


def bloq(): #funcion para bloquear los botones    for i in range(0, 9):
        lisb[i].config(state="disable")   #Coloca a los botones en estado desactivado

def inij(): #Iniciar juego    for i in range(0, 9):
        lisb[i].config(state="normal")  #Coloca los botones en estado normal, activados        lisb[i].config(bg="lightgray")  #Cambia el color de los botones a gris claro        lisb[i].config(text="")     #Limpia el texto de los botones        tab[i] = "N"            #Agrega    global nomj1, nomj2  # indica a que variables queremos acceder globales    nomj1 = simpledialog.askstring("Jugador", "Escribe el nombre del jugador 1: ")  #abre una caja de texto para introducir el nombre    nomj2 = simpledialog.askstring("Jugador", "Escribe el nombre del jugador 2: ")
    turj.set("Turno: " + nomj1)


def cam(num):   #Marca los botones segun el jugador activo y los marca desactivados    global turno, nomj1, nomj2
    if tab[num] == "N" and turno == 0:  #Si el boton es nulo (=N) y el es el turno del jugador 1        lisb[num].config(text="X")      #Cambia el texto del boton a "X"        lisb[num].config(bg="white")    #Cambia el color del boton a blanco        tab[num] = "X"  #Marca el X en el arreglo en el elemento        turno = 1       #Cambia de jugador        turj.set("Turno: " + nomj2)
    elif tab[num] == "N" and turno == 1:
        lisb[num].config(text="O")
        lisb[num].config(bg="lightblue")
        tab[num] = "O"        turno = 0        turj.set("Turno: " + nomj1)
    lisb[num].config(state="disable")
    verif()                             #Verifica el tablero por ganadores

#Se verifican los elementos en el arreglo de las posiciones posibles para tener un ganador
def verif():
    if (tab[0] == "X" and tab[1] == "X" and tab[2] == "X") or (tab[3] == "X" and tab[4] == "X" and tab[5] == "X") or (
                tab[6] == "X" and tab[7] == "X" and tab[8] == "X"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "X" and tab[3] == "X" and tab[6] == "X") or (tab[1] == "X" and tab[4] == "X" and tab[7] == "X") or (
                tab[2] == "X" and tab[5] == "X" and tab[8] == "X"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "X" and tab[4] == "X" and tab[8] == "X") or (tab[2] == "X" and tab[4] == "X" and tab[6] == "X"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "O" and tab[1] == "O" and tab[2] == "O") or (tab[3] == "O" and tab[4] == "O" and tab[5] == "O") or (
                tab[6] == "O" and tab[7] == "O" and tab[8] == "O"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)
    elif (tab[0] == "O" and tab[3] == "O" and tab[6] == "O") or (tab[1] == "O" and tab[4] == "O" and tab[7] == "O") or (
                tab[2] == "O" and tab[5] == "O" and tab[8] == "O"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)
    elif (tab[0] == "O" and tab[4] == "O" and tab[8] == "O") or (tab[2] == "O" and tab[4] == "O" and tab[6] == "O"):
        bloq()
        messagebox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)



#Se crea la ventana con sus dimesiones y titulo.ven = Tk()
ven.geometry("370x460")
ven.title("Juego del gato")
turno = 0
#Variables para los nombres de los jugadoresnomj1 = ""nomj2 = ""
lisb = []  #Lista para la colocar los botones en ventanatab = []    #Arreglo para los introducir los valores de los botones, es decir perteneciente a jugador 1 o 2turj = StringVar()  #Texto  para el Turno del jugador
for i in range(0, 9):
    tab.append("N")

#Se crean los botones, se les dá el formato y un cursor para cuando les pasamos el ratón por encima.
b0 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(0))
lisb.append(b0) #agrega botón a la listab0.place(x=50, y=50)
b1 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(1))
lisb.append(b1)
b1.place(x=150, y=50)
b2 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(2))
lisb.append(b2)
b2.place(x=250, y=50)
b3 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(3))
lisb.append(b3)
b3.place(x=50, y=150)
b4 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(4))
lisb.append(b4)
b4.place(x=150, y=150)
b5 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(5))
lisb.append(b5)
b5.place(x=250, y=150)
b6 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(6))
lisb.append(b6)
b6.place(x=50, y=250)
b7 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(7))
lisb.append(b7)
b7.place(x=150, y=250)
b8 = Button(ven, width=9, height=3, relief=SOLID, cursor="pencil", command=lambda: cam(8))
lisb.append(b8)
b8.place(x=250, y=250)
tue = Label(ven, textvariable=turj).place(x=140, y=10)          #Muestra el turno del jugador en la parte superior de la ventanabini = Button(ven, bg='blue', fg='white', text='Iniciar juego', cursor="sizing", width=15, height=3,
              command=inij).place(x=130, y=360)
bloq() #Funcion que bloquea los botones para el usuario, los pone en modo Disabled
#Crea y posiciona las lineas con Canvas()
linea = Canvas(ven, width=310, height=10)
linea.place(x=30, y=120)
linea.create_line(310, 0, 0, 0, width=25, fill='black')
l2 = Canvas(ven, width=310, height=10)
l2.place(x=30, y=220)
l2.create_line(310, 0, 0, 0, width=25, fill='black')
l3 = Canvas(ven, width=10, height=310)
l3.place(x=130, y=25)
l3.create_line(0, 310, 0, 0, width=25, fill='black')
l4 = Canvas(ven, width=10, height=310)
l4.place(x=230, y=25)
l4.create_line(0, 310, 0, 0, width=25, fill='black')

ven.mainloop()