Refactor the following python 3.12 code. Do not use new libraries. Focus on reducing…
- User
Refactor the following python 3.12 code. Do not use new libraries. Focus on reducing complexity and clean code. Only use one line docstrings. Code: import math import tkinter as tk from tkinter import ttk import ctypes from tkinter import font as tkFont
--- Tkinter App ---
class CalculatorGUI: def init(self, master): self.master = master self.set_dpi_awareness() self.create_widgets() self.configure_grid()
codedef set_dpi_awareness(self): """Set the DPI awareness for high-DPI displays on Windows.""" try: ctypes.windll.shcore.SetProcessDpiAwareness(1) except Exception as e: print(f"Could not set DPI awareness: {e}") def create_widgets(self): """Create and place all the widgets in the main window.""" self.master.title("Calculator") roboto_font = tkFont.Font(family="Roboto Flex", size=16) self.expression_var = tk.StringVar() # Entry field for input self.entry = ttk.Entry(self.master, textvariable=self.expression_var, font=roboto_font, width=50) self.entry.grid(row=0, column=0, padx=5, pady=5, sticky="ew") self.entry.bind("<Return>", self.calculate) # Text widget for displaying history self.history_text = tk.Text(self.master, width=70, height=10, font=roboto_font, wrap="none", state=tk.DISABLED) self.history_text.grid(row=1, column=0, padx=5, pady=5, sticky="nsew") # Scrollbar for the history text widget scrollbar = ttk.Scrollbar(self.master, orient="vertical", command=self.history_text.yview) scrollbar.grid(row=1, column=1, sticky="ns") self.history_text.configure(yscrollcommand=scrollbar.set) # Label for supported functions and constants supported_info = ( "Functions: sqrt, sin, cos, tan, log, ln, exp\n" "Constants: pi, e\n" "Conversions: lb(kg), kg(lb)" ) self.supported_label = ttk.Label(self.master, text=supported_info, font=roboto_font) self.supported_label.grid(row=2, column=0, padx=5, pady=10, sticky="w") def configure_grid(self): """Configure the grid layout to make it responsive.""" self.master.columnconfigure(0, weight=1) self.master.rowconfigure(1, weight=1) def calculate(self, event=None): """Evaluate the expression and display the result.""" expression = self.expression_var.get() result = evaluate_expression(expression) self.display_result(expression, result) self.expression_var.set("") # Clear the input field def display_result(self, expression, result): """Display the result in the history text widget.""" self.history_text.config(state=tk.NORMAL) self.history_text.insert(tk.END, f"{expression} = {result}\n") self.history_text.config(state=tk.DISABLED) self.history_text.see(tk.END) - Assistant