Advertisement
Rodripelto

Calculo_Figuras

Apr 24th, 2024 (edited)
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.96 KB | None | 0 0
  1. """
  2. Importaciones
  3. """
  4. import tkinter as tk
  5. """
  6. Fin Importaciones
  7. """
  8. ################################# CAPA DE PRESENTACIÓN #########################################
  9. """
  10. Funciones para eventos
  11. """
  12. def elemento_seleccionado(seleccion):
  13.     #limpiar_ventana()
  14.     if seleccion == "Circulo":
  15.         vista_circulo()
  16.     elif seleccion == "Triangulo":
  17.         vista_general()
  18.     else: # Si el resto de figuras tiene la misma entradas incluso se puede eliminar el elif anterior, sino habría que ir completando el if con el resto de figuras
  19.         vista_general()
  20. def dato_modificado(evento):
  21.     dato_base = valor_base.get()
  22.     dato_altura = valor_altura.get()
  23.     seleccion = lista.get()
  24.     resultado = calculos(seleccion,dato_base,dato_altura)
  25.     mostrar_resultados(resultado)
  26. """
  27. Fin Funciones para eventos
  28. """
  29. """
  30. Funciones Generales
  31. """
  32. # Función para mostrar los label y entry generales
  33. def vista_general():
  34.     # Label entry entrada datos
  35.     base.config(text='Valor de la base')
  36.     base.place(x=400,y=60,width=100,height=20)
  37.     valor_base.place(x=500,y=60,width=210,height=20)
  38.     altura.place(x=400,y=90,width=100,height=20)
  39.     valor_altura.place(x=500,y=90,width=210,height=20)
  40.     # Label entry resultados  
  41.     area.place(x=400,y=120,width=100,height=20)
  42.     valor_area.place(x=500,y=120,width=210,height=20)
  43.     perimetro.place(x=400,y=150,width=100,height=20)
  44.     valor_perimetro.place(x=500,y=150,width=210,height=20)
  45. # Función para limpiar la ventana de entry y label
  46. def vista_circulo():
  47.     vista_general() # Muestro todos los datos
  48.     base.config(text='Valor del radio') # Cambio el testo del label
  49.     #Oculto lo que no me interesa
  50.     altura.place_forget()
  51.     valor_altura.place_forget()
  52. # Función para mostrar los resultados
  53. def mostrar_resultados(resultado):
  54.     valor_area.config(text=resultado[0])
  55.     valor_perimetro.config(text=resultado[1])
  56. """
  57. Fin Funciones Generales
  58. """
  59. """
  60. Ventana principal
  61. """
  62. ventana =tk.Tk()
  63. ventana.title("Programa en fase beta")
  64. ventana.resizable(False,True)
  65. ventana.geometry("720x1280")
  66.  
  67. #fondo = tk.PhotoImage(file="fondo.gif")
  68. #fondo = fondo.subsample(1,1)
  69. #fondo1 = tk.Label(image=fondo)
  70. #fondo1.place(x=0,y=0,relwidth=1.0,relheight=1.0)
  71.  
  72. bienvenida = tk.Label(ventana,text=' Bienvenido Kevin')
  73. bienvenida.place(x=280,width=190,height=20)
  74. bienvenida.pack
  75.  
  76. lista = tk.StringVar(ventana)
  77. lista.set('Figura')
  78.  
  79. lista1 = tk.StringVar(ventana)
  80. lista1.set('Método')
  81.  
  82. figuras = ['Circulo','Triangulo','Triangulo rectángulo','Rectángulo']
  83. metodo = ['Trigonométrico','Teorema de Pitágoras']
  84.  
  85. opcion =tk.OptionMenu(ventana,lista,*figuras,command= lambda seleccion: elemento_seleccionado(seleccion))
  86. opcion.config(width=20)
  87. opcion.pack(side='left',padx=30,pady=30)
  88.  
  89. opcion2 =tk.OptionMenu(ventana,lista1,*metodo)
  90. opcion2.config(width=20)
  91. opcion2.pack(side='left',padx=30,pady=30)
  92.  
  93. solicitud = tk.Label(ventana,text='Ingresa los valores conocidos',bg='grey')
  94. solicitud.place(x=530,y=30,width=160,height=20)
  95. #solicitud.pack # No es necesario, ya estamos usando place
  96.  
  97. # Label entry entrada datos
  98. base = tk.Label(ventana)
  99. valor_base = tk.Entry(ventana)
  100. valor_base.bind("<KeyRelease>", dato_modificado)
  101. altura = tk.Label(ventana,text='Valor de la altura')
  102. valor_altura = tk.Entry(ventana)
  103. valor_altura.bind("<KeyRelease>", dato_modificado)
  104. # Label entry resultados  
  105. area = tk.Label(ventana,text='Valor del área')
  106. valor_area = tk.Label(ventana,text="aquí se muestra el resultado")
  107. perimetro = tk.Label(ventana,text='Valor del perímetro')
  108. valor_perimetro = tk.Label(ventana,text="aquí se muestra el resultado")
  109.  
  110. """
  111. Fin Ventana principal
  112. """
  113. ################################# CAPA DE TRABAJO #########################################
  114. """
  115. Clases
  116. """
  117. class Figuras:
  118.     def __init__(self, base, altura):
  119.         self.base = base
  120.         self.altura = altura
  121.     def area(self):
  122.         return self.base * self.altura
  123.     def perimetro(self):
  124.         return self.base * 2 + self.altura * 2
  125.  
  126.  
  127. #figuras geométricas
  128. class Triangulo(Figuras):
  129.     # Sobre escribo el método área y heredo el método __init__
  130.     def area(self):
  131.         return self.base * self.altura / 2
  132.     def perimetro(self):
  133.         return "Lo siento, necesito 3 lados"
  134.  
  135. class Circulo(): # No hereda de figura ya que el método init y el método área son diferentes
  136.     def __init__(self, radio):
  137.         self.radio = radio
  138.     def area(self):
  139.         return 3.14159 * self.radio ** 2
  140.     def perimetro(self):    
  141.         return 3.14159 * self.radio * 2
  142. class Triangulo_Rectangulo(Triangulo):
  143.     def perimetro(self):
  144.         return (self.base ** 2 + self.altura ** 2) ** 0.5
  145.  
  146. class Rectangulo(Figuras):
  147.     pass # Como el método init y el método área son iguales solo creo la clase sin añadir nada
  148.        
  149.  
  150. #métodos
  151. class Metodo:
  152.     def __init__(self,trigonometrico,teorema):
  153.         self.trigonometrico = trigonometrico
  154.         self.teorema = teorema
  155. """
  156. Fin Clases
  157. """
  158. """
  159. Funciones Generales
  160. """
  161. def calculos(figura,valor_base,valor_altura):
  162.     try:
  163.         figuras = Figuras(0,0)
  164.         if valor_base: # Si es cadena vacía es falso.
  165.             valor_base = float(valor_base)
  166.             if figura == "Circulo":
  167.                 figuras = Circulo(valor_base)
  168.         if valor_altura and valor_base: # Si es cadena vacía es falso.
  169.             valor_altura = float(valor_altura)
  170.             if figura == "Rectángulo":
  171.                 figuras = Rectangulo(valor_base,valor_altura)
  172.             elif figura == "Triangulo":
  173.                 figuras = Triangulo(valor_base,valor_altura)
  174.             elif figura == "Triangulo rectángulo":
  175.                 figuras = Triangulo_Rectangulo(valor_base,valor_altura)
  176.     except Exception as error:
  177.         print(type(error),error.args,error, sep="\n")
  178.         return "ERROR", "Datos incorrectos"
  179.     else:
  180.         return figuras.area(), figuras.perimetro()
  181. """
  182. Fin Funciones Generales
  183. """
  184.  
  185. ventana.mainloop() # comienza el programa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement