This project utilizes the Tkinter module in Python to implement a simple visual calculator program. Tkinter is a standard GUI (Graphical User Interface) module in Python’s standard library. It provides a set of tools and widgets for creating graphical user interfaces for desktop applications.
This calculator can perform basic arithmetic operations such as addition, subtraction, multiplication, and division, as well as advanced functions like exponentiation and trigonometric calculations. Link to the project code: https://github.com/Kanomace/EE308FZ_SE_A1
This project only requires implementing basic calculator functionality and a partial frontend visualization. In this task, there is no data interaction between the server, application, and database. The Python GUI module, Tkinter, can be used to develop the visualization module for this project.
To gather more information from the internet, I searched on platforms like CSDN and GitHub to find examples of calculator implementations as a reference for this project. Based on those references, I implemented a program interface with a unique style.
V. Design and Implementation Process
Function: (1) The user can use the " +, -, *, / " calculator to add, subtract, multiply, divide and other elementary arithmetic operations. (2) Press " = " button to display the answer of the current formula (3) " sin, cos, tan, ^ " automatically reads the number on the panel after clicking, and displays the calculation result (4) " C " indicates that the input field is cleared and the calculator status is reset.
VI. Code Description
1. Import the necessary modules:
import math
import tkinter as tk
2. Define the functions for different calculator operations:
defcalculate():try:
result =eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defsin():try:
angle =float(entry.get())
result = math.sin(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defcos():try:
angle =float(entry.get())
result = math.cos(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")deftan():try:
angle =float(entry.get())
result = math.tan(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defpower():try:
exponent =float(entry.get())
result = math.pow(10, exponent)
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")
3. Create a Tkinter window for the calculator:
window = tk.Tk()
window.title("Calculator")
4. Create a frame to hold the calculator components:
import math
import tkinter as tk
defcalculate():try:
result =eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defsin():try:
angle =float(entry.get())
result = math.sin(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defcos():try:
angle =float(entry.get())
result = math.cos(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")deftan():try:
angle =float(entry.get())
result = math.tan(math.radians(angle))
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")defpower():try:
exponent =float(entry.get())
result = math.pow(10, exponent)
entry.delete(0, tk.END)
entry.insert(tk.END,str(result))except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END,"Error")
window = tk.Tk()
window.title("Calculator")# Create a Frame to hold the calculator components
frame = tk.Frame(window, padx=10, pady=10)
frame.pack()
entry = tk.Entry(frame, width=30, font=("Arial",14))
entry.grid(row=0, column=0, columnspan=4, pady=10)# Number buttons
buttons =["7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"]
row =1
col =0for button in buttons:
btn = tk.Button(frame, text=button, width=5, font=("Arial",12), command=lambda button=button: entry.insert(tk.END, button))
btn.grid(row=row, column=col, padx=5, pady=5)
col +=1if col >3:
col =0
row +=1# Trigonometric function buttons
sin_btn = tk.Button(frame, text="sin", width=5, font=("Arial",12), command=sin)
sin_btn.grid(row=2, column=4, padx=5, pady=5)
cos_btn = tk.Button(frame, text="cos", width=5, font=("Arial",12), command=cos)
cos_btn.grid(row=3, column=4, padx=5, pady=5)
tan_btn = tk.Button(frame, text="tan", width=5, font=("Arial",12), command=tan)
tan_btn.grid(row=4, column=4, padx=5, pady=5)# Exponentiation button
power_btn = tk.Button(frame, text="^", width=5, font=("Arial",12), command=power)
power_btn.grid(row=5, column=4, padx=5, pady=5)# Calculate button
calc_btn = tk.Button(frame, text="=", width=12, font=("Arial",12), command=calculate)
calc_btn.grid(row=5, column=2, columnspan=2, padx=5, pady=5)# Clear button
clear_btn = tk.Button(frame, text="C", width=12, font=("Arial",12), command=lambda: entry.delete(0, tk.END))
clear_btn.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
window.mainloop()
VI. Displaying Result
VIII. Summary
Through this project, I gained a preliminary understanding of both front-end and back-end development and learned the basics. I also attempted to create a simple visual calculator using Python’s GUI and continuously improved upon it. It allowed me to experience the entire process of a project, from design to completion.
Although it still has some bugs, I plan to gradually refine and enhance it in the future. I aim to add more advanced features such as numeral system conversion and scientific notation, as completing a project gives me a great sense of accomplishment.
A Calculator with a Visual Interface Based on Python
This project utilizes the Tkinter module in Python to implement a simple visual calculator program. Tkinter is a standard GUI (Graphical User Interface) module in Python's standard library. It provides a set of tools and widgets for creating graphical user