Here's a modern, fully functional calculator built with PyQt5 that includes standard operations, memory functions, and a unique feature: a history panel that shows previous calculations. The design is sleek with a dark theme and responsive buttons.
python Copy code
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLineEdit, QLabel, QGridLayout,
QScrollArea, QFrame)
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QFont, QPalette, QColor
class Calculator(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Modern Calculator")
self.setFixedSize(400, 600)
# Set up the main widget and layout
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
# Initialize variables
self.current_input = ""
self.result = 0
self.memory = 0
self.operation = None
self.history = []
# Set up dark theme
self.set_dark_theme()
# Create UI components
self.create_display()
self.create_history_panel()
self.create_buttons()
def set_dark_theme(self):
palette = QPalette()
palette.setColor(QPalette.Window, QColor(30, 30, 30))
palette.setColor(QPalette.WindowText, Qt.white)
palette.setColor(QPalette.Base, QColor(45, 45, 45))
palette.setColor(QPalette.AlternateBase, QColor(30, 30, 30))
palette.setColor(QPalette.ToolTipBase, Qt.white)
palette.setColor(QPalette.ToolTipText, Qt.white)
palette.setColor(QPalette.Text, Qt.white)
palette.setColor(QPalette.Button, QColor(60, 60, 60))
palette.setColor(QPalette.ButtonText, Qt.white)
palette.setColor(QPalette.BrightText, Qt.red)
palette.setColor(QPalette.Link, QColor(42, 130, 218))
palette.setColor(QPalette.Highlight, QColor(42, 130, 218))
palette.setColor(QPalette.HighlightedText, Qt.black)
self.setPalette(palette)
def create_display(self):
# Display for current input
self.display = QLineEdit()
self.display.setReadOnly(True)
self.display.setAlignment(Qt.AlignRight)
self.display.setFont(QFont("Arial", 20))
self.display.setStyleSheet("""
QLineEdit {
background-color: #2D2D2D;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
}
""")
self.layout.addWidget(self.display)
def create_history_panel(self):
# History panel
self.history_frame = QFrame()
self.history_frame.setStyleSheet("background-color: #252525; border-radius: 5px;")
self.history_layout = QVBoxLayout(self.history_frame)
self.history_scroll = QScrollArea()
self.history_scroll.setWidget(self.history_frame)
self.history_scroll.setWidgetResizable(True)
self.history_scroll.setFixedHeight(100)
self.history_scroll.setStyleSheet("""
QScrollArea {
background-color: #252525;
border: none;
}
""")
self.layout.addWidget(self.history_scroll)
def create_buttons(self):
# Button layout
button_layout = QGridLayout()
button_layout.setSpacing(5)
# Button styles
button_style = """
QPushButton {
background-color: #3C3C3C;
color: white;
border: none;
padding: 15px;
border-radius: 5px;
font-size: 16px;
}
QPushButton:hover {
background-color: #505050;
}
QPushButton:pressed {
background-color: #2A2A2A;
}
"""
operator_style = button_style.replace("#3C3C3C", "#FF9500")
# Button definitions
buttons = [
('C', 0, 0), ('(', 0, 1), (')', 0, 2), ('/', 0, 3),
('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), ('M+', 4, 3),
('MC', 5, 0), ('MR', 5, 1), ('M-', 5, 2), ('√', 5, 3)
]
for text, row, col in buttons:
button = QPushButton(text)
if text in ['+', '-', '*', '/', '=', '√']:
button.setStyleSheet(operator_style)
else:
button.setStyleSheet(button_style)
button.clicked.connect(lambda checked, t=text: self.button_clicked(t))
button_layout.addWidget(button, row, col)
if text == '0':
button_layout.addWidget(button, row, col, 1, 2) # Span 0 button across two columns
self.layout.addLayout(button_layout)
def button_clicked(self, text):
if text in '0123456789.':
self.current_input += text
self.display.setText(self.current_input)
elif text in '+-*/':
if self.current_input:
self.result = float(self.current_input)
self.current_input = ""
self.operation = text
self.display.setText(str(self.result) + " " + text)
elif text == '=':
if self.current_input and self.operation:
second_number = float(self.current_input)
if self.operation == '+':
self.result += second_number
elif self.operation == '-':
self.result -= second_number
elif self.operation == '*':
self.result *= second_number
elif self.operation == '/':
if second_number != 0:
self.result /= second_number
else:
self.display.setText("Error: Division by zero")
return
self.add_to_history(f"{self.result - second_number} {self.operation} {second_number} = {self.result}")
self.display.setText(str(self.result))
self.current_input = ""
self.operation = None
elif text == 'C':
self.current_input = ""
self.result = 0
self.operation = None
self.display.setText("")
elif text == '(':
self.current_input += '('
self.display.setText(self.current_input)
elif text == ')':
self.current_input += ')'
self.display.setText(self.current_input)
elif text == 'M+':
if self.current_input:
self.memory += float(self.current_input)
elif text == 'M-':
if self.current_input:
self.memory -= float(self.current_input)
elif text == 'MR':
self.current_input = str(self.memory)
self.display.setText(self.current_input)
elif text == 'MC':
self.memory = 0
elif text == '√':
if self.current_input:
self.result = float(self.current_input) ** 0.5
self.add_to_history(f"√{self.current_input} = {self.result}")
self.display.setText(str(self.result))
self.current_input = ""
def add_to_history(self, calculation):
self.history.append(calculation)
history_label = QLabel(calculation)
history_label.setStyleSheet("color: white; padding: 5px;")
self.history_layout.addWidget(history_label)
self.history_scroll.verticalScrollBar().setValue(
self.history_scroll.verticalScrollBar().maximum()
)
if __name__ == '__main__':
app = QApplication(sys.argv)
calc = Calculator()
calc.show()
sys.exit(app.exec_())
This calculator provides a solid foundation with a modern UI and can be extended based on your needs!