Creating a calculator with a graphical user interface (GUI) is a bit more involved, but it can be a great learning experience. We'll use the tkinter library, which comes with Python by default, to create the GUI. Here's a step-by-step guide:
Import tkinter: Start by importing the tkinter library and creating the main window.
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Calculator")
Create Display: Create a display field where the user inputs and results will be shown.
display = tk.Entry(root, width=20, borderwidth=5)
display.grid(row=0, column=0, columnspan=4)
Define Button Click Function: Create a function that handles button clicks and updates the display.
def button_click(number):
current = display.get()
display.delete(0, tk.END)
display.insert(0, current + str(number))
Create Buttons: Create buttons for numbers (0-9) and basic operations (+, -, *, /).
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row_val = 1
col_val = 0for button in buttons:
tk.Button(root, text=button, padx=20, pady=20,
command=lambda b=button: button_click(b) if b != '=' else calculate()).grid(row=row_val, column=col_val)
col_val += 1if col_val > 3:
col_val = 0
row_val += 1
Implement Calculation: Create a function to perform calculations when the '=' button is clicked.
def calculate():
expression = display.get()
try:
result = eval(expression)
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
Run the GUI: Run the GUI loop to start the calculator application.
root.mainloop()
The complete code should look like this:
import tkinter as tk
def button_click(number):
current = display.get()
display.delete(0, tk.END)
display.insert(0, current + str(number))
def calculate():
expression = display.get()
try:
result = eval(expression)
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
root = tk.Tk()
root.title("Calculator")
display = tk.Entry(root, width=20, borderwidth=5)
display.grid(row=0, column=0, columnspan=4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row_val = 1
col_val = 0for button in buttons:
tk.Button(root, text=button, padx=20, pady=20,
command=lambda b=button: button_click(b) if b != '=' else calculate()).grid(row=row_val, column=col_val)
col_val += 1if col_val > 3:
col_val = 0
row_val += 1
root.mainloop()
Save this code to a .py file and run it using your Python interpreter. It will open a simple calculator GUI with number buttons and basic arithmetic operations. You can expand upon this by adding more functionality, error handling, and a polished design.
Comments