Visual Calculator on account of python

832101317林伟祥 2023-10-08 23:01:13

1.introduction

This blog is a graphical visualization calculator with addition, subtraction, multiplication, division, backspace, zero and other common functions

 

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 ID21125945(MU)/832101317(FZU)

Link to the finished project code: cleartears/web_calculator_python: a web calculator. (github.com)

2.Project Work

PSP Form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning4560
• Estimate5060
Development325385
• Analysis3030
• Design Spec2015
• Design Review817
• Coding Standard128
• Design6072
• Coding100107
• Code Review6075
• Test4050
Reporting100115
• Test Report6263
• Size Measurement1310
• Postmortem & Process Improvement Plan2759
Sum477566

3.Description of problem-solving ideas

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.

Find information

After looking up the relevant information on CSDN, Bilibili, Github, I found that tkinter is very good for building visual interfaces

Design and Implementation process

1.Determine the needs: clarify the basic functions and characteristics of the calculator, such as the types of mathematical operations supported (addition, subtraction, multiplication and division, square roots, etc.), the display format of the display screen (decimal places, scientific counting, etc.), whether continuous calculation is supported.

2.Interface layout: The calculator interface is divided into different areas, such as the number button area, the operator button area, and the display area. Determine the location and size of each area so that components can be added later.

3.Add components: Add components, such as buttons and text boxes, to each area one by one based on the interface layout. You can use the layout manager to better control the arrangement and placement of components.

4.Display processing: The user's input data is displayed on the display of the calculator, and the calculation results are updated to the display. Need to consider the formatting of the data display, such as decimal places, scientific notation, etc.

Code section 


"""
实现带界面的计算器

"""

from tkinter import *
import tkinter.font
from functools import partial


def get_input(entry, argu):
    entry.insert(END, argu)


def backspace(entry):
    input_len = len(entry.get())
    entry.delete(input_len - 1)


def clear(entry):
    entry.delete(0, END)


def calc(entry):
    input = entry.get()
    output = str(eval(input.strip()))
    clear(entry)
    entry.insert(END, output)


def cal():
    root = Tk()
    root.title("Calc")
    root.resizable(0, 0)

    entry_font = tkinter.font.Font(size=12)
    entry = Entry(root, justify="right", font=entry_font)
    entry.grid(row=0, column=0, columnspan=4, sticky=N+W+S+E, padx=5,  pady=5)

    button_font = tkinter.font.Font(size=10, weight=tkinter.font.BOLD)
    button_bg = '#D5E0EE'
    button_active_bg = '#E5E35B'

    myButton = partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground = button_active_bg)

    button7 = myButton(text='7', command=lambda: get_input(entry, '7'))
    button7.grid(row=1, column=0, pady=5)

    button8 = myButton(text='8', command=lambda: get_input(entry, '8'))
    button8.grid(row=1, column=1, pady=5)

    button9 = myButton(text='9', command=lambda: get_input(entry, '9'))
    button9.grid(row=1, column=2, pady=5)

    button10 = myButton(text='+', command=lambda: get_input(entry, '+'))
    button10.grid(row=1, column=3, pady=5)

    button4 = myButton(text='4', command=lambda: get_input(entry, '4'))
    button4.grid(row=2, column=0, pady=5)

    button5 = myButton(text='5', command=lambda: get_input(entry, '5'))
    button5.grid(row=2, column=1, pady=5)

    button6 = myButton(text='6', command=lambda: get_input(entry, '6'))
    button6.grid(row=2, column=2, pady=5)

    button11 = myButton(text='-', command=lambda: get_input(entry, '-'))
    button11.grid(row=2, column=3, pady=5)

    button1 = myButton(text='1', command=lambda: get_input(entry, '1'))
    button1.grid(row=3, column=0, pady=5)

    button2 = myButton(text='2', command=lambda: get_input(entry, '2'))
    button2.grid(row=3, column=1, pady=5)

    button3 = myButton(text='3', command=lambda: get_input(entry, '3'))
    button3.grid(row=3, column=2, pady=5)

    button12 = myButton(text='*', command=lambda: get_input(entry, '*'))
    button12.grid(row=3, column=3, pady=5)

    button0 = myButton(text='0', command=lambda: get_input(entry, '0'))
    button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)

    button13 = myButton(text='.', command=lambda: get_input(entry, '.'))
    button13.grid(row=4, column=2, pady=5)

    button14 = Button(root, text='/', bg=button_bg, padx=10, pady=3,
                      command=lambda: get_input(entry, '/'))
    button14.grid(row=4, column=3, pady=5)

    button15 = Button(root, text='<-', bg=button_bg, padx=10, pady=3,
                      command=lambda: backspace(entry), activebackground=button_active_bg)
    button15.grid(row=5, column=0, pady=5)

    button16 = Button(root, text='C', bg=button_bg, padx=10, pady=3,
                      command=lambda : clear(entry), activebackground=button_active_bg)
    button16.grid(row=5, column=1, pady=5)

    button17 = Button(root, text='=', bg=button_bg, padx=10, pady=3,
                      command=lambda: calc(entry), activebackground=button_active_bg)
    button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)

    root.mainloop()


if __name__ == '__main__':
    cal()

results display

 

4.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.

 

 

 

...全文
39 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

176

社区成员

发帖
与我相关
我的任务
社区描述
梅努斯软件工程
软件工程 高校 福建省·福州市
社区管理员
  • LinQF39
  • Jcandc
  • chjinhuu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧