Visual Calculator Based on Python

I. Introduction

This blog is for EE308FZ to show the implementation of the Visual calculator including the function of addition, subtraction, multiplication, division, clearing, exponentiation and trigonometric function.

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentVisual Calculator
MU STU ID and FZU STU ID21124868(MU)/832101102(FZU)

Link to the finished project code:https://github.com/Kev1nForever/EE308FZ_Visual_Calculator

II. Project Work

PSP Form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning5060
• Estimate5060
Development325385
• Analysis2530
• Design Spec2015
• Design Review1015
• Coding Standard1010
• Design6070
• Coding100120
• Code Review6075
• Test4050
Reporting100115
• Test Report6065
• Size Measurement1010
• Postmortem & Process Improvement Plan3040
Sum475560

Description of problem-solving ideas

1. How I think about the question

In this project, the basic goal is to complete the functions including input numbers and addition, subtraction, multiplication, division, subtraction, and then implement the functions including trigonometric functions and power functions.

Python is very easy and concise for creating visual tasks, so it can be used to accomplish this task.

2. Find Information

After reading and learning related materials on CSDN, Bilibili, Github, I found that tkinter can be used to build visual interfaces, and it is extremely easy to use.

Design and Implementation process

The design of the calculator is divided into several parts:

  • Visual Interface
  • Interactive button logic
  • Calculation logic

Visual Interface:

For this problem, tkinter can help us directly implement the initialization and design of the interface.

# Define interface
window = tkinter.Tk()
window.geometry('450x500+500+200')
window.resizable(False, False)
window.title("Calculator_Kev1n")

# Defines the output text box
contentVar = tkinter.StringVar(window, "")
contentEnter = tkinter.Entry(window, textvariable=contentVar)
contentEnter['state'] = "readonly"
contentEnter.place(x=25, y=30, width=400, height=50)
contentEnter.configure(font=('Arial', 20))
window.configure(bg="#AEEEEE")

The initialization of the interface is shown in the figure below. We can adjust the font, interface size and color.

Interactive button logic

The buttons on the calculator need to be interactive and correctly displayed on the screen, we directly use the Button function in tkinter (onclick). Display results and calculations are controlled by setting different outputs when different buttons are pressed.
It should be noted that when we press ‘=’, the calculation result needs to be obtained directly and displayed in the output box, so ‘=’ is the key to end an operation, and if there is a problem with the calculation logic, error should be displayed.

Calculation logic

After writing a lot of computational logic code by hand, I found that the eval () function is a good way to implement the function of computation without a lot of independent code and can greatly simplify the code. But there are a few things to note:

  1. Clicks a number button(for example:1, 2, 3): Add the corresponding number to the end of the input box.
  2. Clicks the operator button:
  • Four-rule operation(+,-,×,÷): Note that the user should see × and ÷ on the interface, but should use * and / when programming the calculation. Therefore, after clicking the multiply and divide button, it is necessary to make the corresponding replacement before calculation
  • Trigonometric operation(sin,cos,tan): In eval() calculations, it does not directly recognize trigonometric functions such as sin(), which need to be replaced with math.sin(), and when using the calculator, the user is used to inputting an angle, while eval() calculates radians, so it is necessary to convert the angle inputted by the user into radians and then carry out calculations.
  1. Incorrect formula entered: We must give feedback and ideally point out the cause of the error.

在这里插入图片描述

Code Description

A. Appearance and Layout

For the construction of the window, we use tkinter, as described above:

# Define interface
window = tkinter.Tk()
window.geometry('450x500+500+200')
window.resizable(False, False)
window.title("Calculator_Kev1n")

# Defines the output text box
contentVar = tkinter.StringVar(window, "")
contentEnter = tkinter.Entry(window, textvariable=contentVar)
contentEnter['state'] = "readonly"
contentEnter.place(x=25, y=30, width=400, height=50)
contentEnter.configure(font=('Arial', 20))
window.configure(bg="#AEEEEE")

We use a circular approach to set the key and its size and color, reducing code redundancy.
The code design of the key layout is as follows:


# Define the buttons and layout
bvalue = ['(', ')', 'sin', 'cos', 'tan', 
          '7', '8', '9', '×', '^',
          '4', '5', '6', '÷', 'AC',
          '1', '2', '3', '+', 'back',
          '+/-', '0', '.', '-','=']
index = -1
for i in range(5):
    for j in range(5):
        index += 1
        if index > 25:
            break
        d = bvalue[index]
        btnB = tkinter.Button(window, text=d, command=lambda x=d: onclick(x))
        btnB.place(x=25 + j * 70, y=100 + i * 80, width=60, height=60)
        btnB.configure(font=('Arial', 20))
        btnB.configure(bg='white', fg='black')

btnB = tkinter.Button(window, text='=', command=lambda x="=": onclick(x))
btnB.place(x=305, y=420, width=60, height=60)
btnB.configure(font=('Arial', 20))
btnB.configure(bg='#FF6B81', fg='white')

在这里插入图片描述

B. Operation Method

By defining the onclick function with different settings for different key clicks, it displays the formula and calculates it when ‘=’ is clicked.

def onclick(btn):
    nop = ('+', '-', '×', '÷')
    hop = ('sin', 'cos', 'tan', '^')
    content = contentVar.get()

    if content.startswith('.'):
        content = '0' + content

    if btn in '0123456789':
        content += btn

    elif btn == 'back':
        content = content[0:-1]

    elif btn == '.':
        lastPart = re.split(r'\+|-|\*|/', content)[-1]
        if '.' in lastPart:
            tkinter.messagebox.showerror('Error ',' decimal point ')
            return
        content += btn

    elif btn == 'AC':
        content = ''

    elif btn == '(' or btn == ')':
        content += btn

    elif btn == '=':
        content = content.replace('^', '**')
        content = content.replace('÷', '/')
        content = content.replace('×', '*')
        content = str(method(content))
    
    elif btn in nop:
        
        if content.endswith(nop):
            tkinter.messagebox.showerror('Error ',' continuous operator not allowed ')
            return
        
        content += btn

    elif btn in hop:
        
        content += btn

    contentVar.set(content)

It also defines the function that performs the calculations, categorizes the error cases, and replaces the correct operation symbols before the calculation.

def method(result):
    if re.search(r'sin|cos|tan', result):
        result = re.sub(r'(\d+)', r'math.radians(\1)', result)
        result = result.replace("sin(", "math.sin(")
        result = result.replace("cos(", "math.cos(")
        result = result.replace("tan(", "math.tan(")
        result = str(result)
        print(result)
    try:
        x = eval(result)
    except:
       tkinter.messagebox.showerror('Error ',' expression error ')
    return format(x,'.4f')

III. Result Displaying

在这里插入图片描述
The finished product, as shown in the figure, can normally realize the functions of addition, subtraction, multiplication and division, trigonometric functions, power functions and so on.

IV. Summary

This calculator project enabled me to learn a lot about project implementation, including pre-project task analysis, functional planning, development, testing, optimization and modification, and improved my ability to collect information and independent learning, as well as how to optimize the product and consider user habits. At the same time, I have learned a lot of programming knowledge about visualization, and also learned to use GitHub for code version control and development. I will continue to learn about software engineering and hope to make progress.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值