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
Of course! Creating a GUI that mimics the clean, modern look of the iOS calculator is a great project for Python. We'll use the built-in
tkinterlibrary, which means you don't need to install anything extra.The key to the iOS look is:
- A dark theme
- Perfectly round buttons
- Specific color-coding: orange for operators, light gray for functions, and dark gray for numbers.
- A clean, large display
Since standard
tkinterbuttons are rectangular, we'll cleverly use aCanvaswidget to draw our own round buttons, giving us complete control over the appearance.Final Result
Here's a preview of what we'll be building:
The Python Code
Copy and paste the following code into a file named
ios_calculator.pyand run it. I've added detailed comments to explain how each part works.import tkinter as tk from tkinter import font # --- Constants for Styling --- # Color Palette COLOR_BACKGROUND = "#000000" # Black COLOR_DISPLAY_BG = "#000000" # Black COLOR_DISPLAY_TEXT = "#FFFFFF" # White # Button Colors COLOR_LIGHT_GRAY = "#A5A5A5" COLOR_DARK_GRAY = "#333333" COLOR_ORANGE = "#FF9500" COLOR_ORANGE_ACTIVE = "#F5C780" # Lighter orange for when a button is pressed # Text Colors COLOR_BUTTON_TEXT_LIGHT = "#000000" # Black text for light gray buttons COLOR_BUTTON_TEXT_DARK = "#FFFFFF" # White text for dark gray and orange buttons # Fonts FONT_DISPLAY = ("Helvetica", 48) FONT_BUTTON = ("Helvetica", 24, "bold") # Button Sizing BUTTON_SIZE = 80 BUTTON_PADDING = 8 class Calculator(tk.Tk): """ A Python calculator with a GUI styled to look like the iOS calculator. Uses tkinter for the interface and Canvas widgets to create round buttons. """ def __init__(self): super().__init__() # --- Window Setup --- self.title("iOS Calculator") self.geometry("380x550") # Width x Height self.resizable(False, False) self.config(bg=COLOR_BACKGROUND) # --- State Variables --- self.expression = "" self.display_var = tk.StringVar(value="0") # --- UI Creation --- self.create_display() self.create_buttons() def create_display(self): """Creates the calculator's display screen.""" display_frame = tk.Frame(self, bg=COLOR_BACKGROUND) display_frame.pack(expand=True, fill="both", padx=20, pady=(20, 0)) # We use a Label to show the expression. It's right-aligned. display_label = tk.Label( display_frame, textvariable=self.display_var, font=FONT_DISPLAY, fg=COLOR_DISPLAY_TEXT, bg=COLOR_DISPLAY_BG, anchor="e" # Anchor to the East (right) ) display_label.pack(expand=True, fill='both') def create_buttons(self): """Creates the grid of calculator buttons.""" button_frame = tk.Frame(self, bg=COLOR_BACKGROUND) button_frame.pack(expand=True, fill="both", padx=BUTTON_PADDING, pady=BUTTON_PADDING) # Button layout definition # (text, row, col, columnspan, color_type) button_map = [ ('AC', 0, 0, 1, 'light_gray'), ('+/-', 0, 1, 1, 'light_gray'), ('%', 0, 2, 1, 'light_gray'), ('÷', 0, 3, 1, 'orange'), ('7', 1, 0, 1, 'dark_gray'), ('8', 1, 1, 1, 'dark_gray'), ('9', 1, 2, 1, 'dark_gray'), ('×', 1, 3, 1, 'orange'), ('4', 2, 0, 1, 'dark_gray'), ('5', 2, 1, 1, 'dark_gray'), ('6', 2, 2, 1, 'dark_gray'), ('-', 2, 3, 1, 'orange'), ('1', 3, 0, 1, 'dark_gray'), ('2', 3, 1, 1, 'dark_gray'), ('3', 3, 2, 1, 'dark_gray'), ('+', 3, 3, 1, 'orange'), ('0', 4, 0, 2, 'dark_gray'), ('.', 4, 2, 1, 'dark_gray'), ('=', 4, 3, 1, 'orange') ] # Configure grid columns and rows to have equal weight for i in range(5): button_frame.rowconfigure(i, weight=1) if i < 4: button_frame.columnconfigure(i, weight=1) # Create and place each button from the map for (text, row, col, columnspan, color_type) in button_map: self.create_round_button(button_frame, text, row, col, columnspan, color_type) def create_round_button(self, parent, text, row, col, columnspan, color_type): """Creates a custom round button using a Canvas widget.""" # Determine colors based on button type if color_type == 'light_gray': fill_color = COLOR_LIGHT_GRAY text_color = COLOR_BUTTON_TEXT_LIGHT active_fill = "#D3D3D3" # Even lighter gray on press elif color_type == 'dark_gray': fill_color = COLOR_DARK_GRAY text_color = COLOR_BUTTON_TEXT_DARK active_fill = "#777777" # Lighter dark gray else: # Orange fill_color = COLOR_ORANGE text_color = COLOR_BUTTON_TEXT_DARK active_fill = COLOR_ORANGE_ACTIVE # Create a canvas for the round button canvas = tk.Canvas(parent, bg=COLOR_BACKGROUND, width=BUTTON_SIZE, height=BUTTON_SIZE, highlightthickness=0) # The '0' button is wider, so we adjust the canvas width and the oval coordinates if columnspan > 1: canvas.config(width=BUTTON_SIZE * 2 + BUTTON_PADDING * 2) oval_coords = (1, 1, BUTTON_SIZE, BUTTON_SIZE) # Standard oval for left side else: oval_coords = (1, 1, BUTTON_SIZE, BUTTON_SIZE) # Standard circle # Draw the round shape on the canvas button_shape = canvas.create_oval(oval_coords, fill=fill_color, outline=fill_color) # If it's the '0' button, draw a rectangle to make it pill-shaped if columnspan > 1: canvas.create_rectangle(BUTTON_SIZE / 2, 1, BUTTON_SIZE + BUTTON_PADDING*2, BUTTON_SIZE, fill=fill_color, outline=fill_color) canvas.create_oval(BUTTON_SIZE + BUTTON_PADDING*2 - BUTTON_SIZE/2, 1, BUTTON_SIZE * 2 + BUTTON_PADDING*2 - BUTTON_SIZE/2, BUTTON_SIZE, fill=fill_color, outline=fill_color) # Add the text in the center of the canvas text_x = (BUTTON_SIZE + BUTTON_PADDING * 2 * (columnspan - 1)) / 2 text_y = BUTTON_SIZE / 2 + 1 # Small adjustment for vertical centering canvas.create_text(text_x, text_y, text=text, font=FONT_BUTTON, fill=text_color) # Place the canvas on the grid canvas.grid(row=row, column=col, columnspan=columnspan, sticky='nsew', padx=BUTTON_PADDING, pady=BUTTON_PADDING) # --- Event Bindings for Click Animation and Functionality --- # Change color on mouse press canvas.bind("<Button-1>", lambda event: canvas.itemconfig(button_shape, fill=active_fill)) # Call handler and revert color on mouse release canvas.bind("<ButtonRelease-1>", lambda event, t=text: ( self.on_button_press(t), canvas.itemconfig(button_shape, fill=fill_color) )) def on_button_press(self, char): """Handles the logic when a calculator button is pressed.""" if char in "0123456789.": if self.expression == "0" and char != '.': self.expression = char else: self.expression += char elif char == "AC": self.expression = "" elif char == "=": try: # Replace visual operators with Python operators for eval() expr_to_eval = self.expression.replace("×", "*").replace("÷", "/") result = eval(expr_to_eval) # Format result: remove .0 for whole numbers if result == int(result): self.expression = str(int(result)) else: self.expression = str(round(result, 8)) # Round to 8 decimal places except Exception: self.expression = "Error" elif char == "+/-": if self.expression and self.expression != "0": if self.expression.startswith('-'): self.expression = self.expression[1:] else: self.expression = '-' + self.expression elif char == "%": if self.expression: try: self.expression = str(eval(self.expression) / 100) except Exception: self.expression = "Error" # For operators, just append them to the expression else: # '+', '-', '×', '÷' if self.expression and self.expression[-1] not in " +-×÷": self.expression += char self.update_display() def update_display(self): """Updates the display label with the current expression.""" if self.expression == "": self.display_var.set("0") else: # Simple font size adjustment for long numbers if len(self.expression) > 9: FONT_DISPLAY_small = ("Helvetica", 32) self.children['!frame'].children['!label'].config(font=FONT_DISPLAY_small) else: self.children['!frame'].children['!label'].config(font=FONT_DISPLAY) self.display_var.set(self.expression) if __name__ == "__main__": app = Calculator() app.mainloop()How to Run It
- Save the code: Save the code above as a Python file (e.g.,
ios_calculator.py). - Open a terminal or command prompt.
- Navigate to the directory where you saved the file.
- Run the script using the command:
python ios_calculator.py
A window with your new, stylish calculator should appear
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.