Advertisement
teslariu

Untitled

Dec 16th, 2020
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import tkinter as tk
  5. from math import sqrt, factorial, pi
  6.  
  7. ventana = tk.Tk()
  8. ventana.config(width=400, height=600, bg="Light Steel Blue")
  9. ventana.title("Calculadora")
  10. ventana.resizable(0,0)
  11.  
  12. def limpieza():
  13.     global operador
  14.     ingresa_texto.set("0")
  15.     operador =""
  16.    
  17. def clic(tecla):
  18.     global operador
  19.     operador = operador + str(tecla)
  20.     ingresa_texto.set(operador)
  21.    
  22. def operacion():
  23.     global operador
  24.     try:
  25.         total = str(eval(operador))
  26.     except:
  27.         limpieza()
  28.         total = "ERROR"
  29.     ingresa_texto.set(total)
  30.  
  31. ######  Programa principal  #####################
  32.  
  33. # dimensiones de las teclas
  34. ancho = 10
  35. alto = 3
  36. # variable para hacer la operacion matematica
  37. operador = ""
  38. # variable para mostrar en pantalla de la calculadora
  39. ingresa_texto = tk.StringVar()
  40.  
  41. limpieza()
  42.  
  43.  
  44. # pantalla
  45. pantalla = tk.Entry(
  46.                     font = ['arial',20,'bold'],
  47.                     width = 22,
  48.                     textvariable = ingresa_texto,
  49.                     bd=18,
  50.                     bg="powder blue",
  51.                     justify="right",                    
  52.                     state=tk.DISABLED
  53.                     )
  54. pantalla.place(x=10,y=60)
  55.  
  56. # primer fila de teclas: 1 2 3 +
  57. boton1 = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic(1))
  58. boton1.place(x=17,y=180)
  59. boton2 = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic(2))
  60. boton2.place(x=107,y=180)
  61. boton3 = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic(3))
  62. boton3.place(x=197,y=180)
  63. botonSuma = tk.Button(text="+", width=ancho, height=alto, command=lambda:clic("+"))
  64. botonSuma.place(x=287,y=180)
  65.  
  66. # segunda fila de teclas: 4 5 6 -
  67. boton4 = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic(4))
  68. boton4.place(x=17,y=240)
  69. boton5 = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic(5))
  70. boton5.place(x=107,y=240)
  71. boton6 = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic(6))
  72. boton6.place(x=197,y=240)
  73. botonResta = tk.Button(text="-", width=ancho, height=alto, command=lambda:clic("-"))
  74. botonResta.place(x=287,y=240)
  75.  
  76. # tercera fila de teclas: 7 8 9 x
  77. boton7 = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic(7))
  78. boton7.place(x=17,y=300)
  79. boton8 = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic(8))
  80. boton8.place(x=107,y=300)
  81. boton9 = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic(9))
  82. boton9.place(x=197,y=300)
  83. botonPor = tk.Button(text="x", width=ancho, height=alto, command=lambda:clic("*"))
  84. botonPor.place(x=287,y=300)
  85.  
  86. # cuarta fila de teclas: ( 0 ) /
  87. botonParIz = tk.Button(text="(", width=ancho, height=alto, command=lambda:clic("("))
  88. botonParIz.place(x=17,y=360)
  89. boton0 = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
  90. boton0.place(x=107,y=360)
  91. botonParDer = tk.Button(text=")", width=ancho, height=alto, command=lambda:clic(")"))
  92. botonParDer.place(x=197,y=360)
  93. botonDiv = tk.Button(text="/", width=ancho, height=alto, command=lambda:clic("/"))
  94. botonDiv.place(x=287,y=360)
  95.  
  96. # quinta fila de teclas: Raiz Coma Potencia %
  97. botonRaiz = tk.Button(text="RAIZ", width=ancho, height=alto, command=lambda:clic('sqrt('))
  98. botonRaiz.place(x=17,y=420)
  99. botonComa = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  100. botonComa.place(x=107,y=420)
  101. botonPot = tk.Button(text="POWER", width=ancho, height=alto, command=lambda:clic("**"))
  102. botonPot.place(x=197,y=420)
  103. botonResto = tk.Button(text="%", width=ancho, height=alto, command=lambda:clic("%"))
  104. botonResto.place(x=287,y=420)
  105.  
  106. # sexta fila de teclas: Clear Factorial PI =
  107. botonClear = tk.Button(text="CL", width=ancho, bg="cadet blue",height=alto, command=limpieza)
  108. botonClear.place(x=17,y=480)
  109. botonFact = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic('factorial('))
  110. botonFact.place(x=107,y=480)
  111. botonPI = tk.Button(text="PI", width=ancho, height=alto, command=lambda:clic(pi))
  112. botonPI.place(x=197,y=480)
  113. botonIgual = tk.Button(text="=", width=ancho, bg="cadet blue",height=alto, command=operacion)
  114. botonIgual.place(x=287,y=480)
  115.  
  116.  
  117.  
  118. ventana.mainloop()
  119.  
  120.  
Advertisement
RAW Paste Data Copied
Advertisement