lunes, 11 de septiembre de 2017

Práctica con funciones y figuras Tkinter en ventana hija

1. Círculo, rectángulo y línea en vantana hija. (Forma 1: Funciones)
#-*- coding: utf-8 -*-
from Tkinter import  *

ventanaPrincipal = Tk()  # Tk() Es la ventana principal
ventanaPrincipal.title("Ventana principal") # Título de la ventana
ventanaPrincipal.config(bg="orange")  # Le da color al fondo
ventanaPrincipal.geometry("500x400")  # Cambia el tamaño de la ventana

ventanaHija = Toplevel(ventanaPrincipal)
ventanaHija.title("Ventana hija")
ventanaHija.protocol("WM_DELETE_WINDOW", "onexit")

figura = Canvas(ventanaHija)

def ejecutar(f):
    lista = figura.find_all()
    ventanaPrincipal.after(200, f)  # Una forma de ejecutar las funciones
    try:
        figura.delete(figura.find_below(lista[1]))
    except:
        print (figura.find_all())

def circulo(ventana):
    ventanaHija.deiconify()
    figura.config(width=210, height=210,bg='red')  # Lienzo con las medidas y color sobre el que se dibujará la figura
    figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
    figura.create_oval(10, 10, 200, 200, width=3, fill='blue')  # Coordenadas y características de la figura

def rectangulo(ventana):
    ventanaHija.deiconify()
    figura.config(width=210, height=210,bg='white')  # Lienzo con las medidas y color sobre el que se dibujará la figura
    figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
    figura.create_rectangle(10, 10, 200, 200, width=5, fill='yellow')  # Coordenadas y características de la figura

def lineas(ventana):
    ventanaHija.deiconify()
    figura.config(width=210, height=210,bg='gray')  # Lienzo con las medidas y color sobre el que se dibujará la figura
    figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
    figura.create_line(0, 200, 200, 0, width=10, fill='purple')  # Coordenadas y características de la figura

def ocultar(ventana):
    ventanaHija.withdraw()

botonCirculo = Button(ventanaPrincipal, text="Mostrar circulo", command=lambda: ejecutar (circulo(ventanaHija)))  # Primer boton
botonCirculo.grid (row=1, column=1)  # El botón es cargado

botonRectangulo = Button(ventanaPrincipal, text="Mostrar rectangulo", command=lambda: ejecutar (rectangulo(ventanaHija)))  # Primer boton
botonRectangulo.grid (row=1, column=2)  # El botón es cargado

botonLineas = Button(ventanaPrincipal, text="Mostrar linea", command=lambda: ejecutar (lineas(ventanaHija)))  # Primer boton
botonLineas.grid (row=1, column=3)  # El botón es cargado

botonVentanaHija= Button(ventanaHija, text="Cerrar", command=lambda: ejecutar(ocultar(ventanaHija)))
botonVentanaHija.pack()

ventanaHija.withdraw()
ventanaPrincipal.mainloop()




2. Círculo, rectángulo y línea en vantana hija. (Forma 2: Clases y funciones)
#-*- coding: utf-8 -*-
from Tkinter import  *

v = Tk()  # Tk() Es la ventana principal
v.title("Ventana Principal")
v.geometry("500x500")
v.config(bg="brown")
class main(Frame):
    def __init__(self, master=None):
        Frame.__init__(self,master)
        self.pack()

        ventanaHija = Toplevel(self)
        ventanaHija.title("Ventana hija")
        ventanaHija.protocol("WM_DELETE_WINDOW", "onexit")
        ventanaHija.withdraw()
        botonVentanaHija = Button(ventanaHija, text="Cerrar", command=lambda: self.ocultar(ventanaHija))
        botonVentanaHija.pack()

        figura = Canvas(ventanaHija)

        botonCirculo = Button(self, text="Mostrar circulo",command=lambda:self.ejecutar(self.circulo(ventanaHija,figura),ventanaHija,figura))  # Primer boton
        botonCirculo.grid(row=1, column=1)  # El botón es cargado

        botonRectangulo = Button(self, text="Mostrar rectangulo",command=lambda:self.ejecutar(self.rectangulo(ventanaHija,figura),ventanaHija,figura))  # Primer boton
        botonRectangulo.grid(row=1, column=2)  # El botón es cargado

        botonLineas = Button(self, text="Mostrar linea",command=lambda:self.ejecutar(self.lineas(ventanaHija,figura),ventanaHija,figura))  # Primer boton
        botonLineas.grid(row=1, column=3)  # El botón es cargado

    def ejecutar(self,f,ventana,figura):
        lista = figura.find_all()
        ventana.after(200, f)  # Una forma de ejecutar las funciones
        try:
            figura.delete(figura.find_below(lista[1]))
        except:
            print (figura.find_all())

    def circulo(self,ventana,figura):
        ventana.deiconify()
        figura.config(width=210, height=210,bg='red')  # Lienzo con las medidas y color sobre el que se dibujará la figura
        figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
        figura.create_oval(10, 10, 200, 200, width=3, fill='blue')  # Coordenadas y características de la figura

    def rectangulo(self,ventana,figura):
        ventana.deiconify()
        figura.config(width=210, height=210,bg='white')  # Lienzo con las medidas y color sobre el que se dibujará la figura
        figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
        figura.create_rectangle(10, 10, 200, 200, width=5, fill='yellow')  # Coordenadas y características de la figura

    def lineas(self,ventana,figura):
        ventana.deiconify()
        figura.config(width=210, height=210,bg='gray')  # Lienzo con las medidas y color sobre el que se dibujará la figura
        figura.pack(expand=YES, fill=BOTH)  # Sirve para cargar la figura
        figura.create_line(0, 200, 200, 0, width=10, fill='purple')  # Coordenadas y características de la figura

    def ocultar(self,ventana):
        ventana.withdraw()

objeto = main(master=v)
objeto.mainloop()




No hay comentarios:

Publicar un comentario