Improve the text rendering in the GUI. Code: import ctypes
import math
import re
from typing import Any, Dict, 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
#-- Unique Functions --
def capitalize_sentence(sentence: str) -> str:
"""Capitalize the first letter of each word in the sentence."""
return ' '.join(word.capitalize() for word in sentence.split())
#-- Conversions --
def kg_to_lb(kg: float) -> float:
"""Convert kilograms to pounds."""
return kg / 0.45359237
To improve the text rendering in the GUI, you can focus on adjusting fonts, enhancing readability, and ensuring consistent styling across different widgets. Here are some steps and modifications to enhance the text rendering for your GUI:
Set a Uniform and Readable Font:
Ensure that all text uses the same readable font and size. Customize the font size to make it comfortable to read.
Improve Font Definition:
If "Roboto Flex" is not installed, consider using a widely available system font such as "Arial" or "Helvetica" for better compatibility across different systems.
Add Font Weight and Styles:
Use font weight (e.g., bold) for emphasized text, like labels or headers.
Enhance Text Alignment and Margins:
Properly align text and add margins or paddings to avoid clutter, especially in text areas.
Here's an updated version of your code with these improvements:
python
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
#-- Unique Functions --
def capitalize_sentence(sentence: str) -> str:
"""Capitalize the first letter of each word in the sentence."""
return ' '.join(word.capitalize() for word in sentence.split())
#-- Conversions --
def kg_to_lb(kg: float) -> float:
"""Convert kilograms to pounds."""
return kg / 0.45359237
def lb_to_kg(lb: float) -> float:
"""Convert pounds to kilograms."""
return lb * 0.45359237
#-- Processing --
def evaluate_expression(expression: str, use_degrees: bool = False) -> Union[float, str]:
"""Safely evaluate a mathematical expression considering degree mode."""
# ... rest of `evaluate_expression` remains unchanged ...
class CalculatorGUI(QMainWindow):
"""PySide6-based Calculator with Degrees/Radians toggle and history."""
def __init__(self):
super().__init__()
self.use_degrees = False
self.set_dpi_awareness()
self.setup_window()
self.create_widgets()
self.layout_widgets()
def set_dpi_awareness(self) -> None:
"""Enable high-DPI support on Windows."""
try:
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception:
pass
def setup_window(self) -> None:
"""Configure the main application window."""
self.setWindowTitle("Calculator")
self.setGeometry(100, 100, 1200, 720)
def create_widgets(self) -> None:
"""Initialize all GUI components."""
# Improved font styles for clarity and readability
self.font = QFont("Arial", 12)
self.button_font = QFont("Arial", 11, QFont.Bold)
self.entry = QLineEdit()
self.entry.setFont(self.font)
self.entry.returnPressed.connect(self.calculate)
self.entry.setPlaceholderText("Enter expression...")
self.history_text = QTextEdit()
self.history_text.setFont(self.font)
self.history_text.setReadOnly(True)
self.scrollbar = QScrollBar(Qt.Vertical)
self.history_text.setVerticalScrollBar(self.scrollbar)
self.function_buttons = {
"Functions": ["sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "ln"],
"Conversions": ["lb", "kg"],
"Unique": ["capitalize"],
"Constants": ["pi", "e"]
}
self.function_frame = QFrame()
self.create_function_buttons()
def create_function_buttons(self):
"""Create buttons for inserting functions into the entry."""
layout = QGridLayout()
# Add category labels with bold font
categories = ["Functions", "Conversions", "Unique", "Constants", "Settings"]
for col, category in enumerate(categories):
label = QLabel(category, font=self.button_font)
layout.addWidget(label, 0, col * 3 if category == "Functions" else col + 2)
# Add function buttons
for col, category in enumerate(categories):
if category == "Functions":
for i, func in enumerate(self.function_buttons[category]):
button = QPushButton(func)
button.setFont(self.button_font)
button.clicked.connect(lambda checked, f=func: self.insert_function(f, False))
layout.addWidget(button, (i // 3) + 1, i % 3)
elif category == "Settings":
self.mode_button = QPushButton("Radians")
self.mode_button.setFont(self.button_font)
self.mode_button.clicked.connect(self.toggle_mode)
layout.addWidget(self.mode_button, 1, col + 2)
elif category in self.function_buttons:
column = col + 2
functions = self.function_buttons[category]
for i, func in enumerate(functions):
if category == "Conversions":
button_text = f"{func}({'kg' if func == 'lb' else 'lb'})"
else:
button_text = func
is_constant = category == "Constants"
button = QPushButton(button_text)
button.setFont(self.button_font)
button.clicked.connect(lambda checked, f=func, c=is_constant: self.insert_function(f, c))
layout.addWidget(button, i + 1, column)
self.function_frame.setLayout(layout)
def layout_widgets(self) -> None:
"""Arrange widgets within the 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 toggle_mode(self) -> None:
"""Switch between Degrees and Radians modes."""
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) -> None:
"""Insert function or constant text into the expression entry."""
current_text = self.entry.text()
cursor_position = self.entry.cursorPosition()
if is_constant:
new_text = f"{current_text[:cursor_position]}{function}{current_text[cursor_position:]}"
new_cursor_position = cursor_position + len(function)
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 and update history."""
expression = self.entry.text().strip()
if expression:
result = evaluate_expression(expression, use_degrees=self.use_degrees)
self.display_result(expression, result)
self.entry.clear()
def display_result(self, expression: str, result: Union[float, str]) -> None:
"""Append the calculation to the history log."""
mode = "Degrees" if self.use_degrees else "Radians"
self.history_text.append(f"<b>[{mode}]</b> {expression} = <b>{result}</b>")
self.history_text.verticalScrollBar().setValue(
self.history_text.verticalScrollBar().maximum()
)
def main() -> None:
"""Run the Calculator application."""
app = QApplication([])
calculator = CalculatorGUI()
calculator.show()
app.exec()
if __name__ == "__main__":
main()
Key Improvements:
Font Selection: Changed to "Arial" which is more universally available, ensuring broader compatibility.
Font Style and Weight: Texts in the function buttons and labels are in bold for better emphasis.
Placeholder Text: Added to the entry field for user guidance.
HTML Styling in History: Used basic HTML tags for bold rendering of the results in the history log for better visibility.