python
import math
import tkinter as tk
from tkinter import font as tkfont
class ScientificCalculator:
def __init__(self, root):
self.root = root
self.root.title("Calculadora Científica")
self.root.geometry("400x600")
self.root.resizable(False, False)
self.root.configure(bg='#f0f0f0')
# Variables
self.current_input = tk.StringVar()
self.current_input.set("0")
self.operation = None
self.stored_value = 0
self.reset_input = False
# Fuentes personalizadas
self.display_font = tkfont.Font(size=24, weight='bold')
self.button_font = tkfont.Font(size=12)
self.scientific_font = tkfont.Font(size=10)
# Crear interfaz
self.create_display()
self.create_buttons()
def create_display(self):
display_frame = tk.Frame(self.root, bg='#333')
display_frame.pack(fill=tk.X, padx=10, pady=10)
display = tk.Entry(
display_frame,
textvariable=self.current_input,
font=self.display_font,
bd=0,
insertwidth=0,
bg='#333',
fg='white',
justify=tk.RIGHT,
state='readonly'
)
display.pack(fill=tk.X, ipady=20, padx=5, pady=5)
def create_buttons(self):
button_frame = tk.Frame(self.root, bg='#f0f0f0')
button_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)
# Configuración de botones
button_config = [
('C', 1, 0, '#ff5555', self.clear),
('±', 1, 1, '#e0e0e0', self.negate),
('%', 1, 2, '#e0e0e0', self.percentage),
('÷', 1, 3, '#ff9933', lambda: self.set_operation('/')),
('7', 2, 0, '#ffffff', lambda: self.add_digit('7')),
('8', 2, 1, '#ffffff', lambda: self.add_digit('8')),
('9', 2, 2, '#ffffff', lambda: self.add_digit('9')),
('×', 2, 3, '#ff9933', lambda: self.set_operation('*')),
('4', 3, 0, '#ffffff', lambda: self.add_digit('4')),
('5', 3, 1, '#ffffff', lambda: self.add_digit('5')),
('6', 3, 2, '#ffffff', lambda: self.add_digit('6')),
('-', 3, 3, '#ff9933', lambda: self.set_operation('-')),
('1', 4, 0, '#ffffff', lambda: self.add_digit('1')),
('2', 4, 1, '#ffffff', lambda: self.add_digit('2')),
('3', 4, 2, '#ffffff', lambda: self.add_digit('3')),
('+', 4, 3, '#ff9933', lambda: self.set_operation('+')),
('0', 5, 0, '#ffffff', lambda: self.add_digit('0')),
('.', 5, 1, '#ffffff', lambda: self.add_decimal()),
('=', 5, 2, '#ff9933', self.calculate, 2),
# Botones científicos
('sin', 1, 4, '#bbbbff', lambda: self.scientific_op('sin')),
('cos', 1, 5, '#bbbbff', lambda: self.scientific_op('cos')),
('tan', 2, 4, '#bbbbff', lambda: self.scientific_op('tan')),
('π', 2, 5, '#bbbbff', lambda: self.add_constant(math.pi)),
('√', 3, 4, '#bbbbff', lambda: self.scientific_op('sqrt')),
('x²', 3, 5, '#bbbbff', lambda: self.scientific_op('square')),
('log', 4, 4, '#bbbbff', lambda: self.scientific_op('log')),
('ln', 4, 5, '#bbbbff', lambda: self.scientific_op('ln')),
('e', 5, 4, '#bbbbff', lambda: self.add_constant(math.e)),
('!', 5, 5, '#bbbbff', lambda: self.scientific_op('factorial')),
]
# Crear botones
for (text, row, col, bg, command, colspan=1) in button_config:
btn = tk.Button(
button_frame,
text=text,
font=self.scientific_font if row > 0 and col > 3 else self.button_font,
bg=bg,
fg='black',
bd=0,
highlightthickness=0,
command=command
)
btn.grid(
row=row,
column=col,
columnspan=colspan,
sticky=tk.NSEW,
padx=2,
pady=2,
ipady=10
)
button_frame.grid_columnconfigure(col, weight=1)
button_frame.grid_rowconfigure(5, weight=1)
def add_digit(self, digit):
current = self.current_input.get()
if current == '0' or self.reset_input:
self.current_input.set(digit)
self.reset_input = False
else:
self.current_input.set(current + digit)
def add_decimal(self):
current = self.current_input.get()
if '.' not in current:
self.current_input.set(current + '.')
def clear(self):
self.current_input.set('0')
self.operation = None
self.stored_value = 0
def negate(self):
current = float(self.current_input.get())
self.current_input.set(str(-current))
def percentage(self):
current = float(self.current_input.get())
self.current_input.set(str(current / 100))
def set_operation(self, op):
self.stored_value = float(self.current_input.get())
self.operation = op
self.reset_input = True
def calculate(self):
if self.operation:
current = float(self.current_input.get())
try:
if self.operation == '+':
result = self.stored_value + current
elif self.operation == '-':
result = self.stored_value - current
elif self.operation == '*':
result = self.stored_value * current
elif self.operation == '/':
result = self.stored_value / current
self.current_input.set(str(result))
self.operation = None
self.reset_input = True
except ZeroDivisionError:
self.current_input.set("Error")
self.operation = None
def scientific_op(self, op):
current = float(self.current_input.get())
try:
if op == 'sin':
result = math.sin(math.radians(current))
elif op == 'cos':
result = math.cos(math.radians(current))
elif op == 'tan':
result = math.tan(math.radians(current))
elif op == 'sqrt':
result = math.sqrt(current)
elif op == 'square':
result = current ** 2
elif op == 'log':
result = math.log10(current)
elif op == 'ln':
result = math.log(current)
elif op == 'factorial':
result = math.factorial(int(current))
self.current_input.set(str(result))
self.reset_input = True
except ValueError as e:
self.current_input.set("Error")
def add_constant(self, const):
self.current_input.set(str(const))
self.reset_input = True
if __name__ == "__main__":
root = tk.Tk()
calculator = ScientificCalculator(root)
root.mainloop()