lunes, 30 de octubre de 2017

Progarama de figuras con Turtle

# -*- coding: utf-8 -*-
from Tkinter import *
import turtle
import tkSimpleDialog
from tkColorChooser import askcolor
import math

ventanaPrincipal = Tk()
ventanaPrincipal.resizable(False, False)
ventanaPrincipal.title("Ventana principal - Area de dibujo")

canvasLienzo = Canvas(ventanaPrincipal, width=750, height=750)
canvasLienzo.pack(side="left", fill="both", expand=True)

canvasMenu = Canvas(ventanaPrincipal, width=750, height=750)
canvasMenu.pack(side="right", fill="both", expand=True)

turtle_screen = turtle.TurtleScreen(canvasLienzo)
turtle_screen.bgcolor("white")
tortuga=turtle.RawTurtle(turtle_screen)

lapizColor = "black"
rellenoColor = "white"
tortuga.fill(False)
fondoColor = "white"

posicionX=DoubleVar()
posicionY=DoubleVar()

def limpiar():
    tortuga.reset()
    colorLapiz(botonLapiz,"black")
    colorRelleno(botonRelleno,"white")
    colorFondo(botonFondo,"white")

def colorLapiz(self,color):
    self.configure(bg = color)
    tortuga.pencolor(color)

def colorRelleno(self,color):
    self.configure(bg=color)
    tortuga.fill(True)
    print tortuga.fill()
    tortuga.fillcolor(color)

def colorFondo(self,color):
    self.configure(bg=color)
    turtle_screen.bgcolor(color)

def seleccionarColor():
    seleccion = askcolor()
    return seleccion[1]

def tamanioLapiz(event):
    w=event.widget
    if isinstance(w,Scale):
        tortuga.pensize(w.get())

def cambiarCoordenadas():
    try:
        posicionX.set(tkSimpleDialog.askfloat("Posición en X", "Ingresa la coordenada: ", initialvalue=0.0))
        posicionY.set(tkSimpleDialog.askfloat("Posición en Y", "Ingresa la coordenada: ", initialvalue=0.0))
        tortuga.penup()
        tortuga.goto(posicionX.get(),posicionY.get())
        tortuga.pendown()
    except:
        print "Operación cancelada"

def poligono():
    try:
        lados = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el número de lados: ",initialvalue=3,minvalue=3,maxvalue=360)
        linea = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el tamaño de cada lado: ", initialvalue=50,minvalue=1)
        angulo = DoubleVar()
        angulo.set(360.0/lados)
        print tortuga.fill()
        if(tortuga.fill()==True):
            tortuga.begin_fill()
        for i in range(lados):
            tortuga.forward(linea)
            tortuga.left(angulo.get())
        tortuga.end_fill()
        colorRelleno(botonRelleno,"white")
        tortuga.fill(False)
    except:
        print "Operacion cancelada"

def mariposa():
    n = 0
    colores = ["#201815", "#584C4C", "#3F6E80", "#0F4851", "#473A68", "#201815", "#584C4C", "#3F6E80", "#0F4851",
               "#473A68"]
    if (tortuga.fill() == True):
        tortuga.begin_fill()
    for t in range(0, 3600):
        if (t % 360 == 0):
            tortuga.pencolor(colores[n])
            colorLapiz(botonLapiz, colores[n])
            n = n + 1
        t = t * ((math.pi) / 180)
        x = (math.sin(t) * ((math.exp(math.cos(t))) - (2 * math.cos(4 * t)) - ((math.sin(t / 12)) ** 5))) * 60
        y = (math.cos(t) * ((math.exp(math.cos(t))) - (2 * math.cos(4 * t)) - ((math.sin(t / 12)) ** 5))) * 60
        tortuga.goto(x, y)
    tortuga.end_fill()

def repetirPoligonos():
    try:
        lados = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el número de lados: ",initialvalue=3,minvalue=3,maxvalue=360)
        linea = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el tamaño de cada lado: ", initialvalue=50,minvalue=1)
        repeticion = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el número de repeticiones: ", initialvalue=1,minvalue=1, maxvalue=100)
        angulo = tkSimpleDialog.askinteger("Poligono regular", "Ingresa el ángulo de rotación: ",initialvalue=1, minvalue=1, maxvalue=360)

        anguloFigura = DoubleVar()
        anguloFigura.set(360.0/lados)
        print tortuga.fill()

        if (tortuga.fill() == True):
            tortuga.begin_fill()
        for i in range(repeticion):
            tortuga.left(angulo)
            for j in range(lados):
                tortuga.forward(linea)
                tortuga.left(anguloFigura.get())
        tortuga.end_fill()
        colorRelleno(botonRelleno,"white")
        tortuga.fill(False)
    except:
        print "Operacion cancelada"

def espiral():
    tam=25
    tortuga.speed(20)
    for i in range(120):
        tortuga.left(5)
        for j in range(4):
            tortuga.forward(tam)
            tortuga.left(90)
        tam=tam+5

def origen():
    tortuga.penup()
    tortuga.home()
    tortuga.pendown()

def globo():
    aumento = 0
    aumento2=0
    color = ["#c1133d", "#e2e2e2", "#842a40"]
    for i in range(3):
        tortuga.penup()
        tortuga.home()
        tortuga.pendown()

        colorRelleno(botonRelleno,color[i])
        tortuga.fillcolor(color[i])
        tortuga.begin_fill()
        tortuga.forward(30 - aumento)
        tortuga.left(50 + aumento)
        tortuga.forward(120)
        tortuga.left(5)
        tortuga.forward(30)
        tortuga.left(5)
        tortuga.forward(30)
        tortuga.left(5)
        tortuga.forward(30)

        if (i == 2):
            tortuga.left(2)
            tortuga.forward(15)
            tortuga.left(2)
            tortuga.forward(15)
            tortuga.left(2)
            tortuga.forward(15)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(4)
            tortuga.forward(10)
            tortuga.left(4)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.home()
            tortuga.end_fill()
        if (i == 1):
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(15)
            tortuga.forward(15)

            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(3)
            tortuga.forward(10)
            tortuga.left(2)
            tortuga.forward(10)
            tortuga.left(2)
            tortuga.forward(12)
            tortuga.home()
            tortuga.end_fill()
        if (i == 0):
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(10)
            tortuga.forward(30)
            tortuga.left(15)
            tortuga.forward(15)

            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(10)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)
            tortuga.left(5)
            tortuga.forward(15)

            tortuga.end_fill()

        aumento += 10

    for i in range(3):
        tortuga.penup()
        tortuga.home()
        tortuga.pendown()

        colorRelleno(botonRelleno, color[2 - i])
        tortuga.fillcolor(color[2 - i])
        tortuga.begin_fill()

        tortuga.left(180)
        tortuga.forward(30 - aumento2)
        tortuga.right(50 + aumento2)
        tortuga.forward(120)
        tortuga.right(5)
        tortuga.forward(30)
        tortuga.right(5)
        tortuga.forward(30)
        tortuga.right(5)
        tortuga.forward(30)

        if (i == 2):
            tortuga.right(2)
            tortuga.forward(15)
            tortuga.right(2)
            tortuga.forward(15)
            tortuga.right(2)
            tortuga.forward(15)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(4)
            tortuga.forward(10)
            tortuga.right(4)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)

            tortuga.end_fill()
        if (i == 1):
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(15)
            tortuga.forward(14)

            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(3)
            tortuga.forward(10)
            tortuga.right(2)
            tortuga.forward(10)
            tortuga.right(2)
            tortuga.forward(12)

            tortuga.end_fill()
        if (i == 0):
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(10)
            tortuga.forward(30)
            tortuga.right(15)
            tortuga.forward(14)

            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(10)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)
            tortuga.right(5)
            tortuga.forward(15)

            tortuga.end_fill()

        aumento2 += 10

    origen()
    tortuga.goto(2, 373)

    origen()
    colorRelleno(botonRelleno, "#A05150")
    tortuga.fillcolor("#A05150")
    tortuga.begin_fill()
    tortuga.forward(30)
    tortuga.right(120)
    tortuga.forward(15)
    tortuga.right(60)
    tortuga.forward(46)
    tortuga.right(60)
    tortuga.forward(15)
    tortuga.home()
    tortuga.end_fill()

    tortuga.penup()
    tortuga.goto(16, -12)
    tortuga.pensize(3)
    tortuga.pendown()
    tortuga.right(90)
    tortuga.forward(20)
    tortuga.left(160)
    tortuga.forward(20)

    origen()
    tortuga.penup()
    tortuga.goto(-15, -12)
    tortuga.pendown()
    tortuga.right(90)
    tortuga.forward(20)
    tortuga.right(155)
    tortuga.forward(20)

    origen()
    tortuga.penup()
    tortuga.goto(0, -32)
    tortuga.pendown()
    colorLapiz(botonLapiz, "#C96B46")
    tortuga.pencolor("#C96B46")
    tortuga.pensize(4)
    tortuga.forward(15)
    tortuga.right(65)
    tortuga.forward(30)

    origen()
    tortuga.penup()
    tortuga.goto(0, -32)
    tortuga.pendown()
    tortuga.right(180)
    tortuga.forward(15)
    tortuga.left(65)
    tortuga.forward(30)

    origen()
    tortuga.penup()
    tortuga.goto(0, -60)
    tortuga.pendown()
    colorLapiz(botonLapiz, "#000000")
    tortuga.pencolor("#000000")
    colorRelleno(botonRelleno, "#A05150")
    tortuga.fillcolor("#A05150")
    tortuga.begin_fill()
    tortuga.pensize(2)
    tortuga.forward(35)
    tortuga.right(90)
    tortuga.forward(6)
    tortuga.right(90)
    tortuga.forward(70)
    tortuga.right(90)
    tortuga.forward(6)
    tortuga.right(90)
    tortuga.forward(35)
    tortuga.end_fill()

    tortuga.penup()
    tortuga.right(90)
    tortuga.forward(6)
    tortuga.pendown()
    colorRelleno(botonRelleno, "#8D3E35")
    tortuga.fillcolor("#8D3E35")
    tortuga.begin_fill()
    tortuga.left(90)
    tortuga.forward(33)
    tortuga.right(110)
    tortuga.forward(30)
    tortuga.right(70)
    tortuga.forward(45)
    tortuga.right(70)
    tortuga.forward(30)
    tortuga.right(110)
    tortuga.forward(33)
    tortuga.end_fill()


etiquetaT1 = Label(canvasMenu, text="Opciones", justify=CENTER, font="Arial 12")
etiquetaT1.grid(row=1, column=1,columnspan=2)

etiquetaT1 = Label(canvasMenu, text="Lápiz:", font="Arial 10", pady=5)
etiquetaT1.grid(row=2, column=1)

botonLapiz = Button(canvasMenu, bg=lapizColor, command=lambda: colorLapiz(botonLapiz, seleccionarColor()))
botonLapiz.grid(row=2, column=2, sticky="W,E")

etiquetaT2 = Label(canvasMenu, text="Relleno:", font="Arial 10", pady=5)
etiquetaT2.grid(row=3, column=1)

botonRelleno = Button(canvasMenu, bg=rellenoColor, command=lambda: colorRelleno(botonRelleno, seleccionarColor()))
botonRelleno.grid(row=3, column=2, sticky="W,E")

etiquetaT3 = Label(canvasMenu, text="Fondo:", font="Arial 10", pady=5)
etiquetaT3.grid(row=4, column=1)

botonFondo = Button(canvasMenu, bg=fondoColor, command=lambda: colorFondo(botonFondo, seleccionarColor()))
botonFondo.grid(row=4, column=2, sticky="W,E")

etiquetaT4 = Label(canvasMenu, text="Tamaño del lápiz", font="Arial 10", pady=5)
etiquetaT4.grid(row=5, column=1,columnspan=2)

botonEscala = Scale(canvasMenu, orient=HORIZONTAL, from_=1, to=10)
botonEscala.grid(row=6, column=1, sticky="W,E",columnspan=2)
botonEscala.bind('',tamanioLapiz)

botonPoligonos = Button(canvasMenu,text="Poligono regular",command=lambda: poligono())
botonPoligonos.grid(row=7, column=1, sticky="W,E", columnspan=2)

botonRepetir = Button(canvasMenu, text="Repetir Poligonos",command=lambda:repetirPoligonos())
botonRepetir.grid(row=8, column=1, sticky="W,E", columnspan=2)

botonMover = Button(canvasMenu, text="Cambiar coordenadas", command=lambda: cambiarCoordenadas())
botonMover.grid(row=9, column=1, sticky="W,E", columnspan=2,pady=10)

botonGlobo = Button(canvasMenu, text="Dibujo globo", command=lambda: globo())
botonGlobo.grid(row=10, column=1, sticky="W,E", columnspan=2)

botonMariposa = Button(canvasMenu, text="Mariposa", command=lambda: mariposa())
botonMariposa.grid(row=11, column=1, sticky="W,E", columnspan=2)

botonMariposa = Button(canvasMenu, text="Espiral", command=lambda: espiral())
botonMariposa.grid(row=12, column=1, sticky="W,E", columnspan=2)


botonLimpiar = Button(canvasMenu, text="Limpiar lienzo", command=lambda: limpiar())
botonLimpiar.grid(row=19, column=1, sticky="W,E", columnspan=2,pady=10)

mainloop()

No hay comentarios:

Publicar un comentario