A Calculator with a Visual Interface Based on Python


I. Introduction

  • 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

II. Basic Information

The link of my classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface
MU STU ID and FZU STU ID21126356_832101325

III. PSP Table

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning5060
• Estimate5060
Development440480
• Analysis5060
• Design Spec1520
• Design Review2530
• Coding Standard1015
• Design5065
• Coding250240
• Code Review2025
• Test2025
Reporting90110
• Test Repor6080
• Size Measurement2015
• Postmortem & Process Improvement Plan1015
Sum580650

IV. Ideas before Launching the Project

  • 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:

def calculate():
    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")

def sin():
    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")

def cos():
    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")

def tan():
    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")

def power():
    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:

frame = tk.Frame(window, padx=10, pady=10)
frame.pack()

5. Set up the calculator interface:

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 = 0
for 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 += 1
    if 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()

6. Complete code:

import math
import tkinter as tk

def calculate():
    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")

def sin():
    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")

def cos():
    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")

def tan():
    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")

def power():
    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 = 0
for 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 += 1
    if 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值