def setup_window(self) -> None:
"""Set up the main window properties."""
self.setWindowTitle("Calculator")
self.setGeometry(100, 100, 1280, 720)
def create_widgets(self) -> None:
"""Create all widgets used in the application."""
font = QFont()
font.setPointSize(12)
font.setFamily('Literata')
self.entry = QLineEdit()
self.entry.setFont(font)
self.entry.setPlaceholderText('Enter expression')
self.entry.setClearButtonEnabled(True)
self.entry.returnPressed.connect(self.calculate)
self.history_text = QTextEdit()
self.history_text.setFont(font)
self.history_text.setReadOnly(True)
self.history_text.setStyleSheet("position: relative;")
self.scrollbar = QScrollBar(Qt.Vertical)
self.history_text.setVerticalScrollBar(self.scrollbar)
self.search_button = QPushButton("🔍", self.history_text)
self.search_button.setFixedSize(24, 24)
self.search_button.setToolTip("Search History")
self.search_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(0, 120, 215, 200);
color: white;
}
""")
self.search_button.clicked.connect(self.toggle_search)
self.search_button.raise_()
self.vars_button = QPushButton("🧮", self.history_text)
self.vars_button.setFixedSize(24, 24)
self.vars_button.setToolTip("Show Variables")
self.vars_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(0, 150, 136, 200);
color: white;
}
""")
self.vars_button.clicked.connect(self.toggle_vars_overlay)
self.vars_button.raise_()
self.delete_button = QPushButton("🗑️", self.history_text)
self.delete_button.setFixedSize(24, 24)
self.delete_button.setToolTip("Clear History")
self.delete_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(255, 0, 0, 200);
color: white;
}
""")
self.delete_button.clicked.connect(self.clear_history)
self.delete_button.raise_()
self.vars_overlay = QLineEdit(self.history_text)
self.vars_overlay.setReadOnly(True)
self.vars_overlay.setPlaceholderText("a = 1, b = 85, ...")
self.vars_overlay.setFixedHeight(24)
self.vars_overlay.setFixedWidth(300)
self.vars_overlay.setStyleSheet("""
QLineEdit {
border: 1px solid gray;
border-radius: 4px;
padding: 2px 4px;
background-color: white;
}
""")
self.vars_overlay.setVisible(False)
self.vars_delete_button = QPushButton("🗑️", self.vars_overlay)
self.vars_delete_button.setFixedSize(24, 24)
self.vars_delete_button.setToolTip("Clear Variables")
self.vars_delete_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(255, 0, 0, 200);
color: white;
}
""")
self.vars_delete_button.clicked.connect(self.clear_variables)
self.vars_delete_button.setVisible(False)
self.search_bar = QLineEdit(self.history_text)
self.search_bar.setPlaceholderText("Search...")
self.search_bar.setFixedHeight(24)
self.search_bar.setFixedWidth(300)
self.search_bar.setVisible(False)
self.search_bar.setStyleSheet("""
QLineEdit {
border: 1px solid gray;
border-radius: 4px;
padding: 2px 4px;
background-color: white;
}
""")
self.search_bar.textChanged.connect(self.filter_history)
self.search_bar.setClearButtonEnabled(True)
self.function_buttons = {
"Functions": ["sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "ln"],
"Conversions": ["lb", "kg", "K", "C", "F", "CF"],
"Text": ["capitalize", "duplicates"],
"Constants": ["pi", "e"],
"Settings": []
}
self.function_frame = QFrame()
self.create_function_buttons(font)
def create_function_buttons(self, font: QFont) -> None:
"""Create and arrange function buttons."""
layout = QGridLayout()
categories = [("Functions", 3), ("Conversions", 2), ("Text", 2), ("Constants", 2), ("Settings", 1)]
starting_col = 0
for category, col_span in categories:
label = QLabel(category)
label.setFont(font)
layout.addWidget(label, 0, starting_col, 1, col_span, Qt.AlignCenter)
buttons = self.function_buttons.get(category, [])
if category == "Functions":
for i, func in enumerate(buttons):
button = QPushButton(func)
button.setFont(font)
button.clicked.connect(
lambda _, f=func: self.insert_function(f, is_text_function=(f in self.text_functions))
)
layout.addWidget(button, (i // 3) + 1, starting_col + (i % 3))
elif category == "Settings":
self.mode_button = QPushButton("Degrees")
self.mode_button.setFont(font)
self.mode_button.clicked.connect(self.toggle_mode)
layout.addWidget(self.mode_button, 1, starting_col, 1, 1)
elif category == "Conversions":
conversion_mapping = {
"lb": "KilogramToPound",
"kg": "PoundToKilogram",
"K": "CelsiusToKelvin",
"C": "KelvinToCelsius",
"F": "CelsiusToFahrenheit",
"CF": "FahrenheitToCelsius"
}
for idx, func in enumerate(buttons):
display_text = {
"lb": "lb(Kg)",
"kg": "kg(lb)",
"K": "K(°C)",
"C": "°C(K)",
"F": "F(°C)",
"CF": "°C(F)"
}.get(func, func)
button = QPushButton(display_text)
button.setFont(font)
function_to_insert = conversion_mapping.get(func, func)
button.clicked.connect(
lambda _, f=function_to_insert: self.insert_function(f, is_text_function=False)
)
row, col = 1 + idx // 2, starting_col + idx % 2
layout.addWidget(button, row, col)
elif category == "Text":
for idx, func in enumerate(buttons):
button = QPushButton(func)
button.setFont(font)
button.clicked.connect(
lambda _, f=func: self.insert_function(f, is_text_function=True)
)
row, col = 1 + idx // col_span, starting_col + idx % col_span
layout.addWidget(button, row, col)
elif category == "Constants":
for idx, func in enumerate(buttons):
button = QPushButton(func)
button.setFont(font)
button.clicked.connect(
lambda _, f=func: self.insert_function(f, is_constant=True)
)
row, col = 1 + idx // col_span, starting_col + idx % col_span
layout.addWidget(button, row, col)
total_rows = math.ceil(len(buttons) / col_span)
# Fill empty grid spaces with empty labels
for extra in range(col_span * total_rows - len(buttons)):
layout.addWidget(QLabel(""), 1 + extra // col_span, starting_col + extra % col_span)
starting_col += col_span
self.function_frame.setLayout(layout)
def layout_widgets(self) -> None:
"""Arrange all widgets in the main window."""
central_widget = QWidget()
main_layout = QVBoxLayout()
main_layout.addWidget(self.entry)
history_layout = QHBoxLayout()
history_layout.addWidget(self.history_text)
history_layout.addWidget(self.scrollbar)
main_layout.addLayout(history_layout)
main_layout.addWidget(self.function_frame)
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def connect_signals(self) -> None:
"""Connect global signals."""
QApplication.instance().focusChanged.connect(self.on_focus_changed)
def toggle_mode(self) -> None:
"""Toggle between degrees and radians for trigonometric functions."""
self.use_degrees = not self.use_degrees
self.mode_button.setText("Degrees" if self.use_degrees else "Radians")
def insert_function(self, function: str, is_constant: bool = False, is_text_function: bool = False) -> None:
"""Insert the selected function into the entry field."""
current_text = self.entry.text()
cursor_position = self.entry.cursorPosition()
if is_constant:
new_text = f"{current_text[:cursor_position]}{function} "
new_cursor_position = cursor_position + len(function) + 1
elif is_text_function and function in self.text_functions:
new_text = f'{current_text[:cursor_position]}{function}() {current_text[cursor_position:]}'
new_cursor_position = cursor_position + len(function) + 1
else:
new_text = f"{current_text[:cursor_position]}{function}() {current_text[cursor_position:]}"
new_cursor_position = cursor_position + len(function) + 1
self.entry.setText(new_text)
self.entry.setCursorPosition(new_cursor_position)
self.entry.setFocus()
def calculate(self) -> None:
"""Evaluate the expression entered by the user."""
expression = self.entry.text().strip()
if expression:
result = evaluate_expression(
expression, self.variables, use_degrees=self.use_degrees
)
self.display_result(expression, result)
self.entry.clear()
def display_result(self, expression: str, result: Union[float, str]) -> None:
"""Display the result in the history."""
mode = "D" if self.use_degrees else "R"
if re.match(r'^\s*[a-zA-Z_]\w*\s*=\s*.+$', expression):
history_entry = f"[{mode}] {result}"
else:
history_entry = f"[{mode}] {expression} = {result}"
self.history_entries.append(history_entry)
self.refresh_history()
self.history_text.verticalScrollBar().setValue(
self.history_text.verticalScrollBar().maximum()
)
def toggle_search(self) -> None:
"""Toggle the visibility of the search bar."""
if self.search_bar.isVisible():
self.search_bar.setVisible(False)
else:
self.search_bar.setVisible(True)
self.search_bar.setFocus()
self.update_overlay_buttons_position()
def filter_history(self, text: str) -> None:
"""Filter the history based on search text."""
self.refresh_history(text)
def refresh_history(self, filter_text: str = "") -> None:
"""Refresh the history display, optionally filtering results."""
self.history_text.clear()
for entry in self.history_entries:
if filter_text.lower() in entry.lower():
self.history_text.append(entry)
def resizeEvent(self, event) -> None:
"""Handle window resize events."""
super().resizeEvent(event)
self.update_overlay_buttons_position()
def update_overlay_buttons_position(self) -> None:
"""Update the position of overlay buttons based on current window size."""
size = self.history_text.viewport().size()
margin_right = 5
margin_top = 5
spacing = 5
available_width = size.width()
search_offset = self.search_bar.width() + spacing if self.search_bar.isVisible() else 0
self.search_button.move(
available_width - search_offset - self.search_button.width() - margin_right,
margin_top
)
vars_offset = self.vars_overlay.width() + spacing if self.vars_overlay.isVisible() else 0
vars_button_y = self.search_button.y() + self.search_button.height() + spacing
self.vars_button.move(
available_width - vars_offset - self.vars_button.width() - margin_right,
vars_button_y
)
self.delete_button.move(
available_width - self.delete_button.width() - margin_right,
self.vars_button.y() + self.vars_button.height() + spacing
)
if self.vars_overlay.isVisible():
self.vars_overlay.move(
available_width - self.vars_overlay.width() - margin_right,
self.vars_button.y()
)
self.vars_delete_button.move(
self.vars_overlay.width() - self.vars_delete_button.width() - margin_right,
(self.vars_overlay.height() - self.vars_delete_button.height()) // 2
)
self.vars_delete_button.setVisible(True)
if self.search_bar.isVisible():
self.search_bar.move(
available_width - self.search_bar.width() - margin_right,
margin_top
)
def on_focus_changed(self, old: QWidget, new: QWidget) -> None:
"""Handle focus changes to hide overlays when necessary."""
if self.vars_overlay.isVisible() and new not in {self.vars_overlay, self.vars_delete_button, self.vars_button}:
self.vars_overlay.setVisible(False)
self.vars_delete_button.setVisible(False)
self.update_overlay_buttons_position()
if self.search_bar.isVisible() and not self.search_bar.text().strip() and new != self.search_bar:
self.toggle_search()
def showEvent(self, event) -> None:
"""Handle the show event to position overlays correctly."""
super().showEvent(event)
self.update_overlay_buttons_position()
def clear_history(self) -> None:
"""Clear the calculation history."""
self.history_entries.clear()
self.refresh_history()
def toggle_vars_overlay(self) -> None:
"""Toggle the visibility of the variables overlay."""
if self.vars_overlay.isVisible():
self.vars_overlay.setVisible(False)
self.vars_delete_button.setVisible(False)
else:
self.update_vars_overlay()
self.vars_overlay.setVisible(True)
self.vars_delete_button.setVisible(True)
self.update_overlay_buttons_position()
def update_vars_overlay(self) -> None:
"""Update the text displayed in the variables overlay."""
if not self.variables:
self.vars_overlay.setText("No variables assigned.")
else:
vars_text = ", ".join(f"{var} = {value}" for var, value in self.variables.items())
self.vars_overlay.setText(vars_text)
def clear_variables(self) -> None:
"""Clear all assigned variables."""
self.variables.clear()
self.update_vars_overlay()
self.vars_overlay.setVisible(True)
self.vars_delete_button.setVisible(True)
To update the background color of the search button and the variables button (vars_button) to match the delete button (delete_button), you'll need to modify their respective setStyleSheet methods.
Steps to Achieve This:
-
Identify the Current Styles:
- Delete Button (
delete_button):
self.delete_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(255, 0, 0, 200);
color: white;
}
""")
- Search Button (
search_button):
self.search_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(0, 120, 215, 200);
color: white;
}
""")
- Variables Button (
vars_button):
self.vars_button.setStyleSheet("""
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(0, 150, 136, 200);
color: white;
}
""")
-
Update Styles to Match Delete Button:
Replace the setStyleSheet for both search_button and vars_button with the same stylesheet used for delete_button.
-
Optimizing with a Shared Style (Optional):
To maintain consistency and make future updates easier, you can define a shared stylesheet variable and apply it to all three buttons.
Updated Code:
Below is the updated code with the necessary changes. I've also included an optional optimization for better maintainability.
import math
import re
from typing import Any, Union, Callable
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLineEdit, QTextEdit, QScrollBar, QPushButton, QLabel, QFrame,
QGridLayout
)
from PySide6.QtGui import QFont
from PySide6.QtCore import Qt
# ... [Rest of your code remains unchanged] ...
class CalculatorGUI(QMainWindow):
def __init__(self):
super().__init__()
self.use_degrees = True
self.variables = {}
self.history_entries = []
self.text_functions = ['capitalize', 'duplicates']
self.setup_window()
self.create_widgets()
self.layout_widgets()
self.connect_signals()
def setup_window(self) -> None:
"""Set up the main window properties."""
self.setWindowTitle("Calculator")
self.setGeometry(100, 100, 1280, 720)
def create_widgets(self) -> None:
"""Create all widgets used in the application."""
font = QFont()
font.setPointSize(12)
font.setFamily('Literata')
self.entry = QLineEdit()
self.entry.setFont(font)
self.entry.setPlaceholderText('Enter expression')
self.entry.setClearButtonEnabled(True)
self.entry.returnPressed.connect(self.calculate)
self.history_text = QTextEdit()
self.history_text.setFont(font)
self.history_text.setReadOnly(True)
self.history_text.setStyleSheet("position: relative;")
self.scrollbar = QScrollBar(Qt.Vertical)
self.history_text.setVerticalScrollBar(self.scrollbar)
# Define a shared stylesheet for delete, search, and vars buttons
shared_button_style = """
QPushButton {
border: none;
background-color: rgba(255, 255, 255, 200);
font-size: 14px;
border-radius: 12px;
}
QPushButton:hover {
background-color: rgba(255, 0, 0, 200);
color: white;
}
"""
self.search_button = QPushButton("🔍", self.history_text)
self.search_button.setFixedSize(24, 24)
self.search_button.setToolTip("Search History")
self.search_button.setStyleSheet(shared_button_style) # Apply shared style
self.search_button.clicked.connect(self.toggle_search)
self.search_button.raise_()
self.vars_button = QPushButton("🧮", self.history_text)
self.vars_button.setFixedSize(24, 24)
self.vars_button.setToolTip("Show Variables")
self.vars_button.setStyleSheet(shared_button_style) # Apply shared style
self.vars_button.clicked.connect(self.toggle_vars_overlay)
self.vars_button.raise_()
self.delete_button = QPushButton("🗑️", self.history_text)
self.delete_button.setFixedSize(24, 24)
self.delete_button.setToolTip("Clear History")
self.delete_button.setStyleSheet(shared_button_style) # Original delete button style
self.delete_button.clicked.connect(self.clear_history)
self.delete_button.raise_()
self.vars_overlay = QLineEdit(self.history_text)
self.vars_overlay.setReadOnly(True)
self.vars_overlay.setPlaceholderText("a = 1, b = 85, ...")
self.vars_overlay.setFixedHeight(24)
self.vars_overlay.setFixedWidth(300)
self.vars_overlay.setStyleSheet("""
QLineEdit {
border: 1px solid gray;
border-radius: 4px;
padding: 2px 4px;
background-color: white;
}
""")
self.vars_overlay.setVisible(False)
self.vars_delete_button = QPushButton("🗑️", self.vars_overlay)
self.vars_delete_button.setFixedSize(24, 24)
self.vars_delete_button.setToolTip("Clear Variables")
self.vars_delete_button.setStyleSheet(shared_button_style) # Reuse shared style
self.vars_delete_button.clicked.connect(self.clear_variables)
self.vars_delete_button.setVisible(False)
self.search_bar = QLineEdit(self.history_text)
self.search_bar.setPlaceholderText("Search...")
self.search_bar.setFixedHeight(24)
self.search_bar.setFixedWidth(300)
self.search_bar.setVisible(False)
self.search_bar.setStyleSheet("""
QLineEdit {
border: 1px solid gray;
border-radius: 4px;
padding: 2px 4px;
background-color: white;
}
""")
self.search_bar.textChanged.connect(self.filter_history)
self.search_bar.setClearButtonEnabled(True)
self.function_buttons = {
"Functions": ["sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "ln"],
"Conversions": ["lb", "kg", "K", "C", "F", "CF"],
"Text": ["capitalize", "duplicates"],
"Constants": ["pi", "e"],
"Settings": []
}
self.function_frame = QFrame()
self.create_function_buttons(font)
# ... [Rest of your methods remain unchanged] ...
# ... [Rest of your code remains unchanged] ...
def main() -> None:
"""Main function to run the calculator application."""
app = QApplication([])
calculator = CalculatorGUI()
calculator.show()
app.exec()
if __name__ == "__main__":
main()
Explanation of Changes:
-
Shared Stylesheet Definition:
- A
shared_button_style string is defined to hold the common stylesheet for the search, variables, and delete buttons. This ensures consistency and makes it easier to manage styles in the future.
-
Applying Shared Styles:
- Search Button (
search_button):
self.search_button.setStyleSheet(shared_button_style)
- Variables Button (
vars_button):
self.vars_button.setStyleSheet(shared_button_style)
- Variables Delete Button (
vars_delete_button):
self.vars_delete_button.setStyleSheet(shared_button_style)
-
Retaining Original Styles for Other Buttons:
- The delete button retains the original stylesheet assigned using
shared_button_style.
Customization:
If you wish to only change the background color without altering other styles (like border, font size, etc.), adjust the shared_button_style accordingly. For example, to only change the background-color and background-color on hover:
shared_button_style = """
QPushButton {
background-color: rgba(255, 255, 255, 200);
}
QPushButton:hover {
background-color: rgba(255, 0, 0, 200);
}
"""
This approach provides flexibility based on your specific design requirements.
Final Notes:
-
Maintainability: By defining a shared stylesheet, you ensure that any future style changes can be done in one place, reducing the risk of inconsistencies.
-
Consistency: All three buttons will now have the same background behavior, providing a uniform look and feel across your application's UI.
Feel free to adjust the RGBA values to better suit your design preferences.