teslariu

calculaloca

May 22nd, 2021
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import tkinter as tk
  5. from math import sqrt, factorial, pi
  6.  
  7. def limpieza():
  8.     global operador
  9.     ingresa_texto.set("0")
  10.     operador = ""
  11.    
  12. def clic(tecla):
  13.     global operador # el mismo operador de la función principal
  14.     operador = operador + str(tecla)
  15.     ingresa_texto.set(operador)
  16.    
  17. def operacion():
  18.     global operador
  19.     try:
  20.         resultado = str(eval(operador))
  21.     except Exception:
  22.         limpieza()
  23.         resultado = "ERROR"
  24.     finally:
  25.         ingresa_texto.set(resultado)
  26.        
  27.        
  28.  
  29. # Ventana de la calculadora
  30. ventana = tk.Tk()
  31. ventana.title("CALCULADORA")
  32. ventana.config(
  33.             width = 390,
  34.             height = 600,
  35.             bg = "Light Steel Blue"
  36.             )
  37. ventana.resizable(0, 0)
  38.  
  39. # variable que se muestra en pantalla
  40. ingresa_texto = tk.StringVar()
  41.  
  42. # tamaño de las teclas
  43. ancho = 10
  44. alto = 3
  45.  
  46. # inicializo la calculadora
  47. limpieza()
  48.  
  49.  
  50.  
  51.  
  52. ##### pantalla ########
  53.  
  54. pantalla = tk.Entry(
  55.                 font = ["arial", 20, "bold"],
  56.                 width = 22,
  57.                 textvariable = ingresa_texto,
  58.                 bd = 20, # dimension del borde de la pantalla
  59.                 bg = "powder blue",
  60.                 justify = "right",
  61.                 state = tk.DISABLED
  62.                 )
  63. pantalla.place(x=10, y=60)
  64.  
  65. ### 1ª fila de teclas : 1 2 3 + #############
  66. boton1 = tk.Button(text="1",width=ancho,height=alto,command=lambda:clic(1))
  67. boton1.place(x=17,y=180)
  68. boton2 = tk.Button(text="2",width=ancho,height=alto,command=lambda:clic(2))
  69. boton2.place(x=107,y=180)
  70. boton3 = tk.Button(text="3",width=ancho,height=alto,command=lambda:clic(3))
  71. boton3.place(x=197,y=180)
  72. botonSuma = tk.Button(text="+",width=ancho,height=alto,bg="white",command=lambda:clic('+'))
  73. botonSuma.place(x=287,y=180)
  74.  
  75.  
  76. ### 2ª fila de teclas : 4 5 6 - #############
  77. boton4 = tk.Button(text="4",width=ancho,height=alto,command=lambda:clic(4))
  78. boton4.place(x=17,y=240)
  79. boton5 = tk.Button(text="5",width=ancho,height=alto,command=lambda:clic(5))
  80. boton5.place(x=107,y=240)
  81. boton6 = tk.Button(text="6",width=ancho,height=alto,command=lambda:clic(6))
  82. boton6.place(x=197,y=240)
  83. botonResta = tk.Button(text="-",width=ancho,height=alto,bg="white",command=lambda:clic('-'))
  84. botonResta.place(x=287,y=240)
  85.  
  86. ### 3ª fila de teclas : 7 8 9 x #############
  87. boton7 = tk.Button(text="7",width=ancho,height=alto,command=lambda:clic(7))
  88. boton7.place(x=17,y=300)
  89. boton8 = tk.Button(text="8",width=ancho,height=alto,command=lambda:clic(8))
  90. boton8.place(x=107,y=300)
  91. boton9 = tk.Button(text="9",width=ancho,height=alto,command=lambda:clic(9))
  92. boton9.place(x=197,y=300)
  93. botonMult = tk.Button(text="x",width=ancho,height=alto,bg="white",command=lambda:clic('*'))
  94. botonMult.place(x=287,y=300)
  95.  
  96. ### 4ª fila de teclas : ( 0 ) / #############
  97. botonParenIzq = tk.Button(text="(",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('('))
  98. botonParenIzq.place(x=17,y=360)
  99. botonCero = tk.Button(text="0",width=ancho,height=alto,command=lambda:clic(0))
  100. botonCero.place(x=107,y=360)
  101. botonParenDer = tk.Button(text=")",width=ancho,height=alto,bg="gainsboro",command=lambda:clic(')'))
  102. botonParenDer.place(x=197,y=360)
  103. botonDiv = tk.Button(text="/",width=ancho,height=alto,bg="white",command=lambda:clic('/'))
  104. botonDiv.place(x=287,y=360)
  105.  
  106. ### 5ª fila de teclas : Raiz, coma, potencia, % #############
  107. botonRaiz = tk.Button(text="RAIZ",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('sqrt('))
  108. botonRaiz.place(x=17,y=420)
  109. botonComa = tk.Button(text=".",width=ancho,height=alto,command=lambda:clic('.'))
  110. botonComa.place(x=107,y=420)
  111. botonPot = tk.Button(text="POWER",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('**'))
  112. botonPot.place(x=197,y=420)
  113. botonResto = tk.Button(text="%",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('%'))
  114. botonResto.place(x=287,y=420)
  115.  
  116. ### 6ª fila de teclas : CLEAR, Factorial, PI, = #############
  117. botonClear = tk.Button(text="CL",width=ancho,height=alto,bg="gainsboro",command=limpieza)
  118. botonClear.place(x=17,y=480)
  119. botonFact = tk.Button(text="!",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('factorial('))
  120. botonFact.place(x=107,y=480)
  121. botonPI = tk.Button(text="PI",width=ancho,height=alto,bg="gainsboro",command=lambda:clic(pi))
  122. botonPI.place(x=197,y=480)
  123. botonIgual = tk.Button(text="=",width=ancho,height=alto,bg="gainsboro",command=operacion)
  124. botonIgual.place(x=287,y=480)
  125.  
  126. ventana.mainloop()
  127.  
Advertisement
Add Comment
Please, Sign In to add comment