teslariu

calk

Jun 23rd, 2021
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  calculadora
  5. #  
  6. import tkinter as tk
  7. from math import sqrt, pi, factorial
  8.  
  9. def clic(tecla):
  10.     global operador
  11.     operador = operador + str(tecla)
  12.     ingresa_texto.set(operador)
  13.    
  14. def limpieza():
  15.     global operador
  16.     ingresa_texto.set("0")
  17.     operador = ""
  18.    
  19.    
  20. def hacer_cuenta():
  21.     global operador
  22.     try:
  23.         total = str(eval(operador))
  24.     except:
  25.         limpieza()
  26.         total = "ERROR"
  27.    
  28.     ingresa_texto.set(total)
  29.  
  30. ########  programa principal  ################
  31.  
  32. ventana = tk.Tk()
  33. ventana.title("CALCULADORA")
  34. ventana.config(width=390, height=600, bg="Light Steel Blue")
  35. ventana.resizable(0,0)
  36.  
  37.  
  38. # tamaño de las teclas
  39. ancho = 9
  40. alto = 3
  41.  
  42. # variable que almacena la cuenta a realizar
  43. operador = ""
  44.  
  45.  
  46. # variable para mostrar en la pantalla
  47. ingresa_texto = tk.StringVar()
  48.  
  49. limpieza()
  50.  
  51. # pantalla
  52. pantalla = tk.Entry(
  53.                 font = ["arial", 20, "bold"],
  54.                 width = 22,                  # ancho borde
  55.                 bd = 15,                     # grosor borde
  56.                 textvariable = ingresa_texto,
  57.                 justify = "right",
  58.                 bg = "powder blue",
  59.                 state = tk.DISABLED
  60.                 )
  61. pantalla.place(x=10, y=60)
  62.  
  63. # primera fila de teclas: 1 2 3 +
  64. boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic(1))
  65. boton.place(x=17, y=180)
  66. boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic(2))
  67. boton.place(x=107, y=180)
  68. boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic(3))
  69. boton.place(x=197, y=180)
  70. boton = tk.Button(text="+", width=ancho, height=alto, bg="sky blue", command=lambda:clic("+"))
  71. boton.place(x=287, y=180)
  72.  
  73. # segunda fila de teclas: 4 5 6 -
  74. boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic(4))
  75. boton.place(x=17, y=240)
  76. boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic(5))
  77. boton.place(x=107, y=240)
  78. boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic(6))
  79. boton.place(x=197, y=240)
  80. boton = tk.Button(text="-", width=ancho, height=alto, bg="sky blue", command=lambda:clic("-"))
  81. boton.place(x=287, y=240)
  82.  
  83. # tercera fila de teclas: 7 8 9 x
  84. boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic(7))
  85. boton.place(x=17, y=300)
  86. boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic(8))
  87. boton.place(x=107, y=300)
  88. boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic(9))
  89. boton.place(x=197, y=300)
  90. boton = tk.Button(text="x", width=ancho, height=alto, bg="sky blue", command=lambda:clic("*"))
  91. boton.place(x=287, y=300)
  92.  
  93. # cuarta fila de teclas: ( 0 ) /
  94. boton = tk.Button(text="(", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("("))
  95. boton.place(x=17, y=360)
  96. boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
  97. boton.place(x=107, y=360)
  98. boton = tk.Button(text=")", width=ancho, height=alto, bg="aquamarine", command=lambda:clic(")"))
  99. boton.place(x=197, y=360)
  100. boton = tk.Button(text="/", width=ancho, height=alto, bg="sky blue", command=lambda:clic("/"))
  101. boton.place(x=287, y=360)
  102.  
  103. # quinta fila de teclas: RAIZ Punto decimal Potencia %
  104. boton = tk.Button(text="RAIZ", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("sqrt("))
  105. boton.place(x=17, y=420)
  106. boton = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  107. boton.place(x=107, y=420)
  108. boton = tk.Button(text="POT", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("**"))
  109. boton.place(x=197, y=420)
  110. boton = tk.Button(text="%", width=ancho, height=alto, bg="sky blue", command=lambda:clic("%"))
  111. boton.place(x=287, y=420)
  112.  
  113. # sexta fila de teclas: Clear Factorial PI =
  114. boton = tk.Button(text="CL", width=ancho, height=alto, bg="aquamarine", command=limpieza)
  115. boton.place(x=17, y=480)
  116. boton = tk.Button(text="!", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("factorial("))
  117. boton.place(x=107, y=480)
  118. boton = tk.Button(text="PI", width=ancho, height=alto, bg="aquamarine", command=lambda:clic(pi))
  119. boton.place(x=197, y=480)
  120. boton = tk.Button(text="=", width=ancho, height=alto, bg="sky blue", command= hacer_cuenta)
  121. boton.place(x=287, y=480)
  122.  
  123. ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment