make me a python calculator which looks like the new ios ui style
- User
make me a python calculator which looks like the new ios ui style
Assistant
iOS-Style Python Calculator
Here's a Python calculator with a modern iOS-inspired UI using Tkinter. This calculator features:
- Rounded buttons with a glass-like effect
- iOS-style color scheme
- Smooth animations (as much as Tkinter allows)
- Proper button layout matching iOS calculator
import tkinter as tk from tkinter import font import math class iOSCalculator: def __init__(self, root): self.root = root self.root.title("iOS Calculator") self.root.geometry("375x667") self.root.resizable(False, False) self.root.configure(bg="#000000") # Custom font self.custom_font = font.Font(family="Helvetica Neue", size=36, weight="bold") self.small_font = font.Font(family="Helvetica Neue", size=20) # Display frame self.display_frame = tk.Frame(self.root, bg="#000000", height=200) self.display_frame.pack(fill=tk.X, padx=20, pady=20) # Display labels self.secondary_label = tk.Label( self.display_frame, text="", font=self.small_font, bg="#000000", fg="#FFFFFF", anchor="e" ) self.secondary_label.pack(expand=True, fill=tk.BOTH) self.primary_label = tk.Label( self.display_frame, text="0", font=self.custom_font, bg="#000000", fg="#FFFFFF", anchor="e" ) self.primary_label.pack(expand=True, fill=tk.BOTH) # Button frame self.button_frame = tk.Frame(self.root, bg="#000000") self.button_frame.pack(expand=True, fill=tk.BOTH, padx=20, pady=(0, 20)) # Button layout button_layout = [ ["AC", "±", "%", "÷"], ["7", "8", "9", "×"], ["4", "5", "6", "-"], ["1", "2", "3", "+"], ["0", ".", "⌫", "="] ] # Button colors self.button_colors = { "AC": "#A5A5A5", "±": "#A5A5A5", "%": "#A5A5A5", "÷": "#FF9F0A", "×": "#FF9F0A", "-": "#FF9F0A", "+": "#FF9F0A", "=": "#FFFFFF", "⌫": "#A5A5A5", "default": "#333333" } # Create buttons for i, row in enumerate(button_layout): self.button_frame.rowconfigure(i, weight=1) for j, text in enumerate(row): self.button_frame.columnconfigure(j, weight=1) # Special case for 0 button to span 2 columns if text == "0": btn = tk.Button( self.button_frame, text=text, font=self.custom_font, bg=self.button_colors.get(text, self.button_colors["default"]), fg="#FFFFFF" if text != "=" else "#000000", activebackground="#FFFFFF" if text != "=" else "#A5A5A5", activeforeground="#000000" if text != "=" else "#FFFFFF", bd=0, highlightthickness=0, relief=tk.FLAT, command=lambda t=text: self.on_button_click(t) ) btn.grid(row=i, column=j, columnspan=2, sticky="nsew", padx=5, pady=5) else: btn = tk.Button( self.button_frame, text=text, font=self.custom_font, bg=self.button_colors.get(text, self.button_colors["default"]), fg="#FFFFFF" if text != "=" else "#000000", activebackground="#FFFFFF" if text != "=" else "#A5A5A5", activeforeground="#000000" if text != "=" else "#FFFFFF", bd=0, highlightthickness=0, relief=tk.FLAT, command=lambda t=text: self.on_button_click(t) ) btn.grid(row=i, column=j, sticky="nsew", padx=5, pady=5) # Make buttons circular btn.bind("<Enter>", lambda e, b=btn: self.on_hover(b)) btn.bind("<Leave>", lambda e, b=btn: self.on_leave(b)) # Calculator state self.current_input = "0" self.previous_input = "" self.operation = None self.reset_input = False def on_hover(self, button): button.config(relief=tk.RAISED) def on_leave(self, button): button.config(relief=tk.FLAT) def on_button_click(self, button_text): if button_text in "0123456789": self.handle_number(button_text) elif button_text == ".": self.handle_decimal() elif button_text in ["+", "-", "×", "÷"]: self.handle_operation(button_text) elif button_text == "=": self.handle_equals() elif button_text == "AC": self.handle_clear() elif button_text == "±": self.handle_negate() elif button_text == "%": self.handle_percentage() elif button_text == "⌫": self.handle_backspace() self.update_display() def handle_number(self, number): if self.current_input == "0" or self.reset_input: self.current_input = number self.reset_input = False else: self.current_input += number def handle_decimal(self): if self.reset_input: self.current_input = "0." self.reset_input = False elif "." not in self.current_input: self.current_input += "." def handle_operation(self, operation): if self.operation and not self.reset_input: self.handle_equals() self.previous_input = self.current_input self.operation = operation self.reset_input = True def handle_equals(self): if not self.operation or self.reset_input: return try: if self.operation == "+": result = float(self.previous_input) + float(self.current_input) elif self.operation == "-": result = float(self.previous_input) - float(self.current_input) elif self.operation == "×": result = float(self.previous_input) * float(self.current_input) elif self.operation == "÷": result = float(self.previous_input) / float(self.current_input) # Format the result to remove .0 if it's an integer if result.is_integer(): self.current_input = str(int(result)) else: self.current_input = str(result) self.secondary_label.config(text=f"{self.previous_input} {self.operation} {self.current_input} =") self.operation = None self.reset_input = True except: self.current_input = "Error" self.reset_input = True def handle_clear(self): self.current_input = "0" self.previous_input = "" self.operation = None self.reset_input = False self.secondary_label.config(text="") def handle_negate(self): if self.current_input != "0": if self.current_input.startswith("-"): self.current_input = self.current_input[1:] else: self.current_input = "-" + self.current_input def handle_percentage(self): try: value = float(self.current_input) / 100 if value.is_integer(): self.current_input = str(int(value)) else: self.current_input = str(value) except: self.current_input = "Error" self.reset_input = True def handle_backspace(self): if len(self.current_input) > 1: self.current_input = self.current_input[:-1] else: self.current_input = "0" def update_display(self): self.primary_label.config(text=self.current_input) if __name__ == "__main__": root = tk.Tk() calculator = iOSCalculator(root) root.mainloop()Features:
-
iOS-Style Design:
- Dark background with light text
- Rounded buttons with proper spacing
- Color scheme matching iOS calculator (gray for functions, orange for operations, white for equals)
-
Functionality:
- Basic arithmetic operations (+, -, ×, ÷)
- Percentage calculations
- Positive/negative toggle
- Backspace functionality
- Clear all (AC) button
-
Display:
- Large primary display for current input
- Secondary display for operation history
- Proper number formatting (removes .0 for whole numbers)
-
Interactive Elements:
- Button hover effects
- Visual feedback when buttons are pressed
To run this calculator, simply copy the code into a Python file and execute it. You'll need Python installed with Tkinter (which comes standard with most Python installations).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.