import tkinter as tk
from tkinter import messagebox
import math
import sympy as sp

class Calculator:
    def __init__(self, master):
        self.master = master
        self.master.title("Мощный Калькулятор")
        self.master.geometry("400x600")
        self.current_expression = ""
        self.history = []
        self.is_dark_mode = False  # По умолчанию светлая тема
        self.create_widgets()

    def create_widgets(self):
        # Поле ввода
        self.entry = tk.Entry(self.master, width=16, font=("Arial", 24), borderwidth=2, relief="solid")
        self.entry.grid(row=0, column=0, columnspan=4)

        # Кнопки
        buttons = [
            ('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
            ('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
            ('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
            ('0', 4, 0), ('.', 4, 1), ('+', 4, 2), ('=', 4, 3),
            ('C', 5, 0), ('√', 5, 1), ('sin', 5, 2), ('cos', 5, 3),
            ('tan', 6, 0), ('log', 6, 1), ('^', 6, 2), ('x', 6, 3),
            ('(', 7, 0), (')', 7, 1), ('История', 7, 2), ('Очистить историю', 7, 3),
            ('Темная/Светлая', 8, 0, 4)
        ]

        for button in buttons:
            text, row, column = button[0], button[1], button[2]
            colspan = button[3] if len(button) > 3 else 1
            button_widget = tk.Button(self.master, text=text,
                                      command=lambda t=text: self.handle_button_click(t),
                                      height=2, width=8)  # Увеличение ширины кнопок

            button_widget.grid(row=row, column=column, columnspan=colspan)

        self.toggle_theme()  # Применение темы

    def toggle_theme(self):
        self.is_dark_mode = not self.is_dark_mode
        bg_color = "black" if self.is_dark_mode else "white"
        fg_color = "white" if self.is_dark_mode else "black"
        self.master.configure(bg=bg_color)

        self.entry.configure(bg=fg_color, fg=bg_color)
        for widget in self.master.winfo_children():
            if isinstance(widget, tk.Button):
                widget.configure(bg=fg_color, fg=bg_color)
                widget.configure(activebackground=bg_color, activeforeground=fg_color)

    def handle_button_click(self, value):
        if value == '=':
            self.calculate_result()
        elif value == 'C':
            self.clear_expression()
        elif value == '√':
            self.square_root()
        elif value in ['sin', 'cos', 'tan', 'log']:
            self.add_function(value)
        elif value == 'История':
            self.show_history()
        elif value == 'Очистить историю':
            self.clear_history()
        elif value == 'Темная/Светлая':
            self.toggle_theme()
        else:
            self.add_to_expression(value)

    def add_to_expression(self, value):
        self.current_expression += str(value)
        self.update_entry()

    def clear_expression(self):
        self.current_expression = ""
        self.update_entry()

    def square_root(self):
        try:
            value = float(self.current_expression)
            result = math.sqrt(value)
            self.current_expression = str(result)
            self.update_entry()
            self.add_to_history(self.current_expression)
        except ValueError:
            self.show_error()

    def add_function(self, func):
        if func == 'sin':
            self.current_expression = f"math.sin({self.current_expression})"
        elif func == 'cos':
            self.current_expression = f"math.cos({self.current_expression})"
        elif func == 'tan':
            self.current_expression = f"math.tan({self.current_expression})"
        elif func == 'log':
            self.current_expression = f"math.log({self.current_expression})"
        self.update_entry()

    def calculate_result(self):
        try:
            expression = self.current_expression.replace('^', '**')
            result = sp.sympify(expression).evalf()
            self.current_expression = str(result)
            self.update_entry()
            self.add_to_history(self.current_expression)
        except Exception:
            self.show_error()

    def update_entry(self):
        self.entry.delete(0, tk.END)
        self.entry.insert(0, self.current_expression)

    def add_to_history(self, result):
        self.history.append(result)

    def show_history(self):
        history_str = "\n".join(self.history)
        messagebox.showinfo("История вычислений", history_str if history_str else "История пуста.")

    def clear_history(self):
        self.history.clear()
        messagebox.showinfo("История вычислений", "История очищена.")

    def show_error(self):
        messagebox.showerror("Ошибка", "Некорректный ввод!")

# Запуск приложения
if __name__ == "__main__":
    root = tk.Tk()
    calculator = Calculator(root)
    root.mainloop()
Мощнее, кнопки шире используй другие библиотеке для красоты